isaacscript-common 1.2.201 → 1.2.204

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 (46) hide show
  1. package/dist/classes/ModUpgraded.d.ts +1 -3
  2. package/dist/classes/ModUpgraded.lua +2 -36
  3. package/dist/enums/private/SerializationBrand.d.ts +2 -0
  4. package/dist/enums/private/SerializationBrand.lua +2 -0
  5. package/dist/features/deployJSONRoom.d.ts +6 -12
  6. package/dist/features/deployJSONRoom.lua +17 -19
  7. package/dist/features/saveDataManager/merge.d.ts +12 -3
  8. package/dist/features/saveDataManager/merge.lua +16 -11
  9. package/dist/functions/array.d.ts +7 -7
  10. package/dist/functions/array.lua +22 -23
  11. package/dist/functions/cards.d.ts +6 -7
  12. package/dist/functions/cards.lua +14 -14
  13. package/dist/functions/color.d.ts +20 -1
  14. package/dist/functions/color.lua +89 -10
  15. package/dist/functions/deepCopy.d.ts +8 -12
  16. package/dist/functions/deepCopy.lua +13 -29
  17. package/dist/functions/doors.d.ts +4 -0
  18. package/dist/functions/doors.lua +12 -2
  19. package/dist/functions/entity.lua +4 -4
  20. package/dist/functions/jsonRoom.d.ts +1 -1
  21. package/dist/functions/jsonRoom.lua +6 -5
  22. package/dist/functions/map.d.ts +0 -12
  23. package/dist/functions/map.lua +0 -23
  24. package/dist/functions/random.d.ts +25 -31
  25. package/dist/functions/random.lua +34 -33
  26. package/dist/functions/rng.d.ts +45 -0
  27. package/dist/functions/rng.lua +106 -0
  28. package/dist/functions/roomData.d.ts +5 -0
  29. package/dist/functions/roomData.lua +31 -4
  30. package/dist/functions/set.d.ts +2 -2
  31. package/dist/functions/set.lua +6 -6
  32. package/dist/functions/spawnCollectible.d.ts +3 -3
  33. package/dist/functions/spawnCollectible.lua +11 -12
  34. package/dist/functions/table.d.ts +30 -0
  35. package/dist/functions/table.lua +73 -1
  36. package/dist/functions/utils.d.ts +5 -0
  37. package/dist/functions/utils.lua +12 -0
  38. package/dist/functions/vector.d.ts +24 -3
  39. package/dist/functions/vector.lua +72 -21
  40. package/dist/index.d.ts +1 -0
  41. package/dist/index.lua +8 -0
  42. package/dist/objects/roomShapeToDoorSlots.d.ts +4 -0
  43. package/dist/objects/roomShapeToDoorSlots.lua +44 -0
  44. package/dist/upgradeMod.d.ts +1 -3
  45. package/dist/upgradeMod.lua +2 -5
  46. package/package.json +1 -1
@@ -1,15 +1,94 @@
1
1
  --[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
2
2
  local ____exports = {}
3
- function ____exports.copyColor(self, color)
4
- return Color(
5
- color.R,
6
- color.G,
7
- color.B,
8
- color.A,
9
- color.RO,
10
- color.GO,
11
- color.BO
12
- )
3
+ local OBJECT_NAME
4
+ local ____SerializationBrand = require("enums.private.SerializationBrand")
5
+ local SerializationBrand = ____SerializationBrand.SerializationBrand
6
+ local ____SerializationType = require("enums.SerializationType")
7
+ local SerializationType = ____SerializationType.SerializationType
8
+ local ____table = require("functions.table")
9
+ local copyValuesToTable = ____table.copyValuesToTable
10
+ local getNumbersFromTable = ____table.getNumbersFromTable
11
+ local ____utils = require("functions.utils")
12
+ local ensureAllCases = ____utils.ensureAllCases
13
+ local isUserdataObject = ____utils.isUserdataObject
14
+ function ____exports.isColor(self, object)
15
+ return isUserdataObject(nil, object, OBJECT_NAME)
16
+ end
17
+ local KEYS = {
18
+ "R",
19
+ "G",
20
+ "B",
21
+ "A",
22
+ "RO",
23
+ "GO",
24
+ "BO"
25
+ }
26
+ OBJECT_NAME = "Color"
27
+ function ____exports.copyColor(self, color, serializationType)
28
+ if serializationType == nil then
29
+ serializationType = SerializationType.NONE
30
+ end
31
+ repeat
32
+ local ____switch3 = serializationType
33
+ local ____cond3 = ____switch3 == SerializationType.NONE
34
+ if ____cond3 then
35
+ do
36
+ if not ____exports.isColor(nil, color) then
37
+ error(((("Failed to copy a " .. OBJECT_NAME) .. " object since the provided object was not a userdata ") .. OBJECT_NAME) .. " class.")
38
+ end
39
+ return Color(
40
+ color.R,
41
+ color.G,
42
+ color.B,
43
+ color.A,
44
+ color.RO,
45
+ color.GO,
46
+ color.BO
47
+ )
48
+ end
49
+ end
50
+ ____cond3 = ____cond3 or ____switch3 == SerializationType.SERIALIZE
51
+ if ____cond3 then
52
+ do
53
+ if not ____exports.isColor(nil, color) then
54
+ error(((("Failed to serialize a " .. OBJECT_NAME) .. " object since the provided object was not a userdata ") .. OBJECT_NAME) .. " class.")
55
+ end
56
+ local colorTable = {}
57
+ copyValuesToTable(nil, color, KEYS, colorTable)
58
+ colorTable[SerializationBrand.COLOR] = ""
59
+ return colorTable
60
+ end
61
+ end
62
+ ____cond3 = ____cond3 or ____switch3 == SerializationType.DESERIALIZE
63
+ if ____cond3 then
64
+ do
65
+ local colorType = type(color)
66
+ if ____exports.isColor(nil, color) or colorType ~= "table" then
67
+ error(("Failed to deserialize a " .. OBJECT_NAME) .. " object since the provided object was not a Lua table.")
68
+ end
69
+ local r, g, b, a, ro, go, bo = table.unpack(getNumbersFromTable(
70
+ nil,
71
+ color,
72
+ OBJECT_NAME,
73
+ table.unpack(KEYS)
74
+ ))
75
+ return Color(
76
+ r,
77
+ g,
78
+ b,
79
+ a,
80
+ ro,
81
+ go,
82
+ bo
83
+ )
84
+ end
85
+ end
86
+ do
87
+ do
88
+ return ensureAllCases(nil, serializationType)
89
+ end
90
+ end
91
+ until true
13
92
  end
14
93
  function ____exports.copyKColor(self, kColor)
15
94
  return KColor(kColor.Red, kColor.Green, kColor.Blue, kColor.Alpha)
@@ -6,22 +6,18 @@ import { SerializationType } from "../enums/SerializationType";
6
6
  *
7
7
  * It supports the following object types:
8
8
  *
9
- * - LuaTables / basic TSTL objects
10
- * - TSTL Maps
11
- * - TSTL Sets
12
- * - TSTL Classes
13
- * - DefaultMaps
14
- * - Isaac Vectors
9
+ * - `LuaTable` / basic TSTL objects
10
+ * - TSTL `Map`
11
+ * - TSTL `Set`
12
+ * - TSTL classes
13
+ * - `DefaultMap`
14
+ * - Isaac `RNG` objects
15
+ * - Isaac `Vector` objects
15
16
  *
16
17
  * @param oldObject The object to copy.
17
18
  * @param serializationType Has 3 possible values. Can leave TypeScriptToLua objects as-is, or can
18
- * serialize objects to Lua tables, or can deserialize Lua tables to objects. Defaults to
19
+ * serialize objects to Lua tables, or can deserialize Lua tables to objects. Default is
19
20
  * `SerializationType.NONE`.
20
21
  * @param traversalDescription Used to track the current key that we are operating on.
21
22
  */
22
23
  export declare function deepCopy(oldObject: LuaTable | Map<AnyNotNil, unknown> | Set<AnyNotNil>, serializationType?: SerializationType, traversalDescription?: string): LuaTable | Map<AnyNotNil, unknown> | Set<AnyNotNil>;
23
- /**
24
- * Used to determine is the given table is a serialized Vector created by the save data manager
25
- * and/or the `deepCopy` function.
26
- */
27
- export declare function isSerializedVector(object: unknown): boolean;
@@ -5,7 +5,7 @@ local Map = ____lualib.Map
5
5
  local __TS__InstanceOf = ____lualib.__TS__InstanceOf
6
6
  local __TS__Iterator = ____lualib.__TS__Iterator
7
7
  local ____exports = {}
8
- local isTSTLClass, copyClass, getNewClassFromMetatable, deepCopyValue, copyVector, checkMetatable, validateValue, TSTL_CLASS_KEYS
8
+ local isTSTLClass, copyClass, getNewClassFromMetatable, deepCopyValue, checkMetatable, validateValue, TSTL_CLASS_KEYS
9
9
  local ____DefaultMap = require("classes.DefaultMap")
10
10
  local DefaultMap = ____DefaultMap.DefaultMap
11
11
  local ____SerializationBrand = require("enums.private.SerializationBrand")
@@ -17,10 +17,15 @@ local ____constants = require("features.saveDataManager.constants")
17
17
  local SAVE_DATA_MANAGER_DEBUG = ____constants.SAVE_DATA_MANAGER_DEBUG
18
18
  local ____log = require("functions.log")
19
19
  local log = ____log.log
20
+ local ____rng = require("functions.rng")
21
+ local copyRNG = ____rng.copyRNG
22
+ local isRNG = ____rng.isRNG
23
+ local isSerializedRNG = ____rng.isSerializedRNG
20
24
  local ____utils = require("functions.utils")
21
25
  local getTraversalDescription = ____utils.getTraversalDescription
22
26
  local ____vector = require("functions.vector")
23
- local deserializeVector = ____vector.deserializeVector
27
+ local copyVector = ____vector.copyVector
28
+ local isSerializedVector = ____vector.isSerializedVector
24
29
  local isVector = ____vector.isVector
25
30
  function ____exports.deepCopy(self, oldObject, serializationType, traversalDescription)
26
31
  if serializationType == nil then
@@ -202,12 +207,10 @@ function deepCopyValue(self, oldObject, newObject, key, value, traversalDescript
202
207
  end
203
208
  end
204
209
  local newValue
205
- if isVector(nil, value) then
206
- local vector = value
207
- newValue = copyVector(nil, vector, serializationType)
208
- elseif ____exports.isSerializedVector(nil, value) and serializationType == SerializationType.DESERIALIZE then
209
- local serializedVector = value
210
- newValue = deserializeVector(nil, serializedVector)
210
+ if isRNG(nil, value) or isSerializedRNG(nil, value) and serializationType == SerializationType.DESERIALIZE then
211
+ newValue = copyRNG(nil, value, serializationType)
212
+ elseif isVector(nil, value) or isSerializedVector(nil, value) and serializationType == SerializationType.DESERIALIZE then
213
+ newValue = copyVector(nil, value, serializationType)
211
214
  elseif valueType == "table" then
212
215
  local ____table = value
213
216
  traversalDescription = getTraversalDescription(nil, key, traversalDescription)
@@ -224,40 +227,21 @@ function deepCopyValue(self, oldObject, newObject, key, value, traversalDescript
224
227
  newObject[keyToUse] = newValue
225
228
  end
226
229
  end
227
- function copyVector(self, vector, serializationType)
228
- if serializationType == SerializationType.SERIALIZE then
229
- local vectorTable = {}
230
- vectorTable.X = vector.X
231
- vectorTable.Y = vector.Y
232
- vectorTable[SerializationBrand.VECTOR] = ""
233
- return vectorTable
234
- end
235
- local newVector = Vector(vector.X, vector.Y)
236
- return newVector
237
- end
238
230
  function checkMetatable(self, ____table, traversalDescription)
239
231
  local metatable = getmetatable(____table)
240
232
  if metatable == nil then
241
233
  return
242
234
  end
243
235
  local tableDescription = traversalDescription == "" and "the table to copy" or ("\"" .. traversalDescription) .. "\""
244
- error(("The deepCopy function detected that " .. tableDescription) .. " has a metatable. Copying tables with metatables is not supported (unless they are TypeScriptToLua Maps, Sets, or Classes).")
236
+ error(("The deepCopy function detected that \"" .. tableDescription) .. "\" has a metatable. Copying tables with metatables is not supported, unless they are explicitly handled by the save data manager. (e.g. Vectors, TypeScriptToLua Maps, etc.)")
245
237
  end
246
238
  function validateValue(self, value, valueType, traversalDescription)
247
239
  if isVector(nil, value) then
248
240
  return
249
241
  end
250
242
  if valueType == "function" or valueType == "nil" or valueType == "thread" or valueType == "userdata" then
251
- error(((("The deepCopy function detected that \"" .. traversalDescription) .. "\" is type ") .. valueType) .. ", which is not supported.")
252
- end
253
- end
254
- function ____exports.isSerializedVector(self, object)
255
- local objectType = type(object)
256
- if objectType ~= "table" then
257
- return false
243
+ error(((("The deepCopy function detected that \"" .. traversalDescription) .. "\" is of type \"") .. valueType) .. "\", which is not supported.")
258
244
  end
259
- local ____table = object
260
- return ____table[SerializationBrand.VECTOR] ~= nil and ____table.X ~= nil and ____table.Y ~= nil
261
245
  end
262
246
  TSTL_CLASS_KEYS = __TS__New(Set, {"____constructor", "__index", "constructor"})
263
247
  return ____exports
@@ -13,6 +13,8 @@ export declare function getDevilRoomDoor(): GridEntityDoor | undefined;
13
13
  * lowest slot number.
14
14
  */
15
15
  export declare function getDevilRoomOrAngelRoomDoor(): GridEntityDoor | undefined;
16
+ /** Helper function to get the possible door slots that can exist for a given room shape. */
17
+ export declare function getDoorSlotsForRoomShape(roomShape: RoomShape): ReadonlySet<DoorSlot>;
16
18
  /**
17
19
  * Helper function to get all of the doors in the room. By default, it will return every door. You
18
20
  * can optionally specify one or more room types to return only the doors that match the specified
@@ -29,6 +31,8 @@ export declare function getDoorsToRoomIndex(...roomGridIndex: int[]): GridEntity
29
31
  export declare function getRepentanceDoor(): GridEntityDoor | undefined;
30
32
  export declare function isAngelRoomDoor(door: GridEntityDoor): boolean;
31
33
  export declare function isDevilRoomDoor(door: GridEntityDoor): boolean;
34
+ /** Helper function to see if a door slot could exist for a given room shape. */
35
+ export declare function isDoorSlotInRoomShape(doorSlot: DoorSlot, roomShape: RoomShape): boolean;
32
36
  /**
33
37
  * This refers to the Repentance door that spawns in a boss room after defeating the boss.
34
38
  * You have to spend one key to open it.
@@ -11,6 +11,8 @@ local ____constants = require("constants")
11
11
  local MAX_NUM_DOORS = ____constants.MAX_NUM_DOORS
12
12
  local ____doorSlotToDirection = require("objects.doorSlotToDirection")
13
13
  local DOOR_SLOT_TO_DIRECTION = ____doorSlotToDirection.DOOR_SLOT_TO_DIRECTION
14
+ local ____roomShapeToDoorSlots = require("objects.roomShapeToDoorSlots")
15
+ local ROOM_SHAPE_TO_DOOR_SLOTS = ____roomShapeToDoorSlots.ROOM_SHAPE_TO_DOOR_SLOTS
14
16
  function ____exports.getDoors(self, ...)
15
17
  local roomTypes = {...}
16
18
  local room = game:GetRoom()
@@ -22,13 +24,13 @@ function ____exports.getDoors(self, ...)
22
24
  do
23
25
  local door = room:GetDoor(i)
24
26
  if door == nil then
25
- goto __continue11
27
+ goto __continue12
26
28
  end
27
29
  if #roomTypes == 0 or roomTypesSet:has(door.TargetRoomType) then
28
30
  __TS__ArrayPush(doors, door)
29
31
  end
30
32
  end
31
- ::__continue11::
33
+ ::__continue12::
32
34
  i = i + 1
33
35
  end
34
36
  end
@@ -95,6 +97,10 @@ function ____exports.getDevilRoomOrAngelRoomDoor(self)
95
97
  end
96
98
  return ____temp_2
97
99
  end
100
+ function ____exports.getDoorSlotsForRoomShape(self, roomShape)
101
+ local doorSlots = ROOM_SHAPE_TO_DOOR_SLOTS[roomShape]
102
+ return doorSlots == nil and __TS__New(Set) or doorSlots
103
+ end
98
104
  function ____exports.getDoorsToRoomIndex(self, ...)
99
105
  local roomGridIndex = {...}
100
106
  local roomGridIndexesSet = __TS__New(Set, roomGridIndex)
@@ -117,6 +123,10 @@ end
117
123
  function ____exports.isDevilRoomDoor(self, door)
118
124
  return door.TargetRoomType == RoomType.ROOM_DEVIL
119
125
  end
126
+ function ____exports.isDoorSlotInRoomShape(self, doorSlot, roomShape)
127
+ local doorSlots = ____exports.getDoorSlotsForRoomShape(nil, roomShape)
128
+ return doorSlots:has(doorSlot)
129
+ end
120
130
  function ____exports.isDoorToDownpour(self, door)
121
131
  if not ____exports.isRepentanceDoor(nil, door) then
122
132
  return false
@@ -10,7 +10,8 @@ local ____storyBossesSet = require("sets.storyBossesSet")
10
10
  local STORY_BOSSES_SET = ____storyBossesSet.STORY_BOSSES_SET
11
11
  local ____random = require("functions.random")
12
12
  local getRandom = ____random.getRandom
13
- local nextSeed = ____random.nextSeed
13
+ local ____rng = require("functions.rng")
14
+ local newRNG = ____rng.newRNG
14
15
  local ____utils = require("functions.utils")
15
16
  local ____repeat = ____utils["repeat"]
16
17
  function ____exports.removeEntities(self, entities, cap)
@@ -136,13 +137,12 @@ function ____exports.rerollEnemy(self, entity)
136
137
  end
137
138
  function ____exports.setEntityRandomColor(self, entity)
138
139
  local colorValues = {}
139
- local seed = entity.InitSeed
140
+ local rng = newRNG(nil, entity.InitSeed)
140
141
  ____repeat(
141
142
  nil,
142
143
  3,
143
144
  function()
144
- seed = nextSeed(nil, seed)
145
- local randomColorValue = getRandom(nil, seed)
145
+ local randomColorValue = getRandom(nil, rng)
146
146
  __TS__ArrayPush(colorValues, randomColorValue)
147
147
  end
148
148
  )
@@ -9,4 +9,4 @@ export declare function getJSONRoomsOfSubType(jsonRooms: JSONRoom[], subType: in
9
9
  * properly account for each room weight using the algorithm from:
10
10
  * https://stackoverflow.com/questions/1761626/weighted-random-numbers
11
11
  */
12
- export declare function getRandomJSONRoom(jsonRooms: JSONRoom[], seed?: Seed, verbose?: boolean): JSONRoom;
12
+ export declare function getRandomJSONRoom(jsonRooms: JSONRoom[], rng?: RNG, verbose?: boolean): JSONRoom;
@@ -9,7 +9,8 @@ local ____log = require("functions.log")
9
9
  local log = ____log.log
10
10
  local ____random = require("functions.random")
11
11
  local getRandomFloat = ____random.getRandomFloat
12
- local getRandomSeed = ____random.getRandomSeed
12
+ local ____rng = require("functions.rng")
13
+ local newRNG = ____rng.newRNG
13
14
  function getTotalWeightOfJSONRooms(self, jsonRooms)
14
15
  local weights = __TS__ArrayMap(
15
16
  jsonRooms,
@@ -65,9 +66,9 @@ function ____exports.getJSONRoomsOfSubType(self, jsonRooms, subType)
65
66
  end
66
67
  )
67
68
  end
68
- function ____exports.getRandomJSONRoom(self, jsonRooms, seed, verbose)
69
- if seed == nil then
70
- seed = getRandomSeed(nil)
69
+ function ____exports.getRandomJSONRoom(self, jsonRooms, rng, verbose)
70
+ if rng == nil then
71
+ rng = newRNG(nil)
71
72
  end
72
73
  if verbose == nil then
73
74
  verbose = false
@@ -76,7 +77,7 @@ function ____exports.getRandomJSONRoom(self, jsonRooms, seed, verbose)
76
77
  if verbose then
77
78
  log("Total weight of the JSON rooms provided: " .. tostring(totalWeight))
78
79
  end
79
- local chosenWeight = getRandomFloat(nil, 0, totalWeight, seed)
80
+ local chosenWeight = getRandomFloat(nil, 0, totalWeight, rng)
80
81
  if verbose then
81
82
  log("Randomly chose weight for JSON room: " .. tostring(chosenWeight))
82
83
  end
@@ -1,12 +1,5 @@
1
- /// <reference types="isaac-typescript-definitions" />
2
- import { DefaultMap } from "../classes/DefaultMap";
3
1
  /** Helper function to copy a map. (You can also use a Map constructor to accomplish this task.) */
4
2
  export declare function copyMap<K, V>(oldMap: Map<K, V>): Map<K, V>;
5
- /**
6
- * Helper function to make using default maps with `Seed` values easier. Use this instead of
7
- * manually getting the current value, incrementing it, and then setting it.
8
- */
9
- export declare function defaultMapGetNextSeed<K, A extends unknown[]>(map: DefaultMap<K, Seed, A>, key: K, ...extraArgs: A): Seed;
10
3
  /**
11
4
  * Helper function to get the closest value from a map based on partial search text. For the
12
5
  * purposes of this function, both search text and map keys are converted to lowercase before
@@ -26,8 +19,3 @@ export declare function defaultMapGetNextSeed<K, A extends unknown[]>(map: Defau
26
19
  * ```
27
20
  */
28
21
  export declare function getMapPartialMatch<T>(searchText: string, map: ReadonlyMap<string, T>): [string, T] | undefined;
29
- /**
30
- * Helper function to make using maps with `Seed` values easier. Use this instead of manually
31
- * getting the current value, incrementing it, and then setting it.
32
- */
33
- export declare function mapGetNextSeed<K>(map: Map<K, Seed>, key: K): Seed | undefined;
@@ -8,8 +8,6 @@ local __TS__StringReplaceAll = ____lualib.__TS__StringReplaceAll
8
8
  local __TS__StringStartsWith = ____lualib.__TS__StringStartsWith
9
9
  local __TS__ArrayFilter = ____lualib.__TS__ArrayFilter
10
10
  local ____exports = {}
11
- local ____random = require("functions.random")
12
- local nextSeed = ____random.nextSeed
13
11
  function ____exports.copyMap(self, oldMap)
14
12
  local newMap = __TS__New(Map)
15
13
  for ____, ____value in __TS__Iterator(oldMap:entries()) do
@@ -19,15 +17,6 @@ function ____exports.copyMap(self, oldMap)
19
17
  end
20
18
  return newMap
21
19
  end
22
- function ____exports.defaultMapGetNextSeed(self, map, key, ...)
23
- local seed = map:getAndSetDefault(key, ...)
24
- if seed == 0 then
25
- error("The seed map had a value of 0 for the key of: " .. tostring(key))
26
- end
27
- local newSeed = nextSeed(nil, seed)
28
- map:set(key, newSeed)
29
- return newSeed
30
- end
31
20
  function ____exports.getMapPartialMatch(self, searchText, map)
32
21
  local keys = {__TS__Spread(map:keys())}
33
22
  __TS__ArraySort(keys)
@@ -51,16 +40,4 @@ function ____exports.getMapPartialMatch(self, searchText, map)
51
40
  end
52
41
  return {matchingKey, value}
53
42
  end
54
- function ____exports.mapGetNextSeed(self, map, key)
55
- local seed = map:get(key)
56
- if seed == nil then
57
- return nil
58
- end
59
- if seed == 0 then
60
- error("The seed map had a value of 0 for the key of: " .. tostring(key))
61
- end
62
- local newSeed = nextSeed(nil, seed)
63
- map:set(key, newSeed)
64
- return newSeed
65
- end
66
43
  return ____exports
@@ -3,54 +3,48 @@
3
3
  * This returns a random float between 0 and 1. It is inclusive on the low end, but exclusive on
4
4
  * the high end. (This is because the `RNG.RandomFloat` method can return a value of 0.999, but it
5
5
  * will never return a value of exactly 1.)
6
+ *
7
+ * Note that this function will invoke the `Next` method on the `RNG` object before returning the
8
+ * random number.
6
9
  */
7
- export declare function getRandom(seed?: Seed): float;
10
+ export declare function getRandom(rng?: RNG): float;
8
11
  /**
9
12
  * This returns a random float between min and max. It is inclusive on the low end, but exclusive on
10
13
  * the high end. (This is because the `RNG.RandomFloat` method can return a value of 0.999, but it
11
14
  * will never return a value of exactly 1.)
12
15
  *
16
+ * Note that this function will invoke the `Next` method on the `RNG` object before returning the
17
+ * random number.
18
+ *
13
19
  * Example:
14
20
  * ```ts
15
- * const realNumberBetweenOneAndThree = getRandomFloat(1, 3, seed);
21
+ * const realNumberBetweenOneAndThree = getRandomFloat(1, 3);
16
22
  * ```
17
23
  */
18
- export declare function getRandomFloat(min: int, max: int, seed?: Seed): float;
24
+ export declare function getRandomFloat(min: int, max: int, rng?: RNG): float;
19
25
  /**
20
- * This returns a random integer between min and max, inclusive.
21
- *
22
- * Example:
23
- * ```ts
24
- * const oneTwoOrThree = getRandomInt(1, 3, seed);
25
- * ```
26
+ * This returns a random float between min and max. It is inclusive on the low end, but exclusive on
27
+ * the high end. (This is because the `RNG.RandomFloat` method can return a value of 0.999, but it
28
+ * will never return a value of exactly 1.)
26
29
  */
27
- export declare function getRandomInt(min: int, max: int, seed?: Seed): int;
30
+ export declare function getRandomFloatFromSeed(min: int, max: int, seed: Seed): float;
28
31
  /**
29
- * Helper function to get a random `Seed` value to be used in spawning entities and so on. Use this
30
- * instead of calling the `Random` function directly since that can return a value of 0 and crash
31
- * the game.
32
+ * This returns a random float between 0 and 1. It is inclusive on the low end, but exclusive on
33
+ * the high end. (This is because the `RNG.RandomFloat` method can return a value of 0.999, but it
34
+ * will never return a value of exactly 1.)
32
35
  */
33
- export declare function getRandomSeed(): Seed;
36
+ export declare function getRandomFromSeed(seed: Seed): float;
34
37
  /**
35
- * Helper function to initialize an RNG object.
38
+ * This returns a random integer between min and max. It is inclusive on both ends.
39
+ *
40
+ * Note that this function will invoke the `Next` method on the `RNG` object before returning the
41
+ * random number.
36
42
  *
37
43
  * Example:
38
44
  * ```ts
39
- * const startSeed = Game():GetSeeds():GetStartSeed();
40
- * const rng = initRNG(startSeed);
41
- * const fiftyFiftyChance = rng.RandomInt(2) === 0;
45
+ * const oneTwoOrThree = getRandomInt(1, 3);
42
46
  * ```
43
- *
44
- * It is recommended to not deal with RNG objects directly and instead use seeds. Also see the
45
- * `getRandom`, `getRandomInt`, and `getRandomFloat` helper functions.
46
- *
47
- * @param seed The seed to initialize it with. Default is `getRandomSeed()`.
48
- */
49
- export declare function initRNG(seed?: Seed): RNG;
50
- /**
51
- * Helper function to get the next seed value.
52
- *
53
- * This function is useful because it is standard practice to work with seed values directly over
54
- * RNG objects, since the latter are not serializable.
55
47
  */
56
- export declare function nextSeed(seed: Seed): Seed;
48
+ export declare function getRandomInt(min: int, max: int, rng?: RNG): int;
49
+ /** This returns a random integer between min and max. It is inclusive on both ends. */
50
+ export declare function getRandomIntFromSeed(min: int, max: int, seed: Seed): int;
@@ -1,46 +1,47 @@
1
1
  --[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
2
2
  local ____exports = {}
3
- local RECOMMENDED_SHIFT_IDX
4
- function ____exports.getRandomSeed(self)
5
- local random = Random()
6
- local safeRandomValue = random == 0 and 1 or random
7
- return safeRandomValue
3
+ local ____rng = require("functions.rng")
4
+ local newRNG = ____rng.newRNG
5
+ function ____exports.getRandom(self, rng)
6
+ if rng == nil then
7
+ rng = newRNG(nil)
8
+ end
9
+ return rng:RandomFloat()
8
10
  end
9
- function ____exports.initRNG(self, seed)
10
- if seed == nil then
11
- seed = ____exports.getRandomSeed(nil)
11
+ function ____exports.getRandomFloat(self, min, max, rng)
12
+ if rng == nil then
13
+ rng = newRNG(nil)
12
14
  end
13
- if seed == 0 then
14
- error("You cannot initialize an RNG object with a seed of 0, or the game will crash.")
15
+ if min > max then
16
+ local oldMin = min
17
+ local oldMax = max
18
+ min = oldMax
19
+ max = oldMin
15
20
  end
16
- local rng = RNG()
17
- rng:SetSeed(seed, RECOMMENDED_SHIFT_IDX)
18
- return rng
21
+ return min + ____exports.getRandom(nil, rng) * (max - min)
19
22
  end
20
- RECOMMENDED_SHIFT_IDX = 35
21
- function ____exports.getRandom(self, seed)
22
- if seed == nil then
23
- seed = ____exports.getRandomSeed(nil)
24
- end
25
- local rng = ____exports.initRNG(nil, seed)
26
- return rng:RandomFloat()
23
+ function ____exports.getRandomFloatFromSeed(self, min, max, seed)
24
+ local rng = newRNG(nil, seed)
25
+ return ____exports.getRandomFloat(nil, min, max, rng)
27
26
  end
28
- function ____exports.getRandomFloat(self, min, max, seed)
29
- if seed == nil then
30
- seed = ____exports.getRandomSeed(nil)
31
- end
32
- return min + ____exports.getRandom(nil, seed) * (max - min)
27
+ function ____exports.getRandomFromSeed(self, seed)
28
+ local rng = newRNG(nil, seed)
29
+ return ____exports.getRandom(nil, rng)
33
30
  end
34
- function ____exports.getRandomInt(self, min, max, seed)
35
- if seed == nil then
36
- seed = ____exports.getRandomSeed(nil)
31
+ function ____exports.getRandomInt(self, min, max, rng)
32
+ if rng == nil then
33
+ rng = newRNG(nil)
34
+ end
35
+ if min > max then
36
+ local oldMin = min
37
+ local oldMax = max
38
+ min = oldMax
39
+ max = oldMin
37
40
  end
38
- local rng = ____exports.initRNG(nil, seed)
39
41
  return rng:RandomInt(max - min + 1) + min
40
42
  end
41
- function ____exports.nextSeed(self, seed)
42
- local rng = ____exports.initRNG(nil, seed)
43
- rng:Next()
44
- return rng:GetSeed()
43
+ function ____exports.getRandomIntFromSeed(self, min, max, seed)
44
+ local rng = newRNG(nil, seed)
45
+ return ____exports.getRandomInt(nil, min, max, rng)
45
46
  end
46
47
  return ____exports
@@ -0,0 +1,45 @@
1
+ /// <reference types="isaac-typescript-definitions" />
2
+ /// <reference types="typescript-to-lua/language-extensions" />
3
+ import { SerializationType } from "../enums/SerializationType";
4
+ declare type SerializedRNG = LuaTable<string, string | number>;
5
+ interface CopyRNGReturn {
6
+ [SerializationType.NONE]: RNG;
7
+ [SerializationType.SERIALIZE]: SerializedRNG;
8
+ [SerializationType.DESERIALIZE]: RNG;
9
+ }
10
+ /**
11
+ * Helper function to copy an `RNG` object.
12
+ *
13
+ * @param rng The RNG object to copy. In the case of deserialization, this will actually be a Lua
14
+ * table instead of an instantiated RNG class.
15
+ * @param serializationType Default is `SerializationType.NONE`.
16
+ */
17
+ export declare function copyRNG<R extends RNG | SerializedRNG, S extends SerializationType>(rng: R, serializationType: S): CopyRNGReturn[S];
18
+ export declare function copyRNG<R extends RNG | SerializedRNG>(rng: R): CopyRNGReturn[SerializationType.NONE];
19
+ /**
20
+ * Helper function to get a random `Seed` value to be used in spawning entities and so on. Use this
21
+ * instead of calling the `Random` function directly since that can return a value of 0 and crash
22
+ * the game.
23
+ */
24
+ export declare function getRandomSeed(): Seed;
25
+ /** Helper function to check if something is an instantiated RNG object. */
26
+ export declare function isRNG(object: unknown): object is RNG;
27
+ /**
28
+ * Used to determine is the given table is a serialized Vector created by the save data manager
29
+ * and/or the `deepCopy` function.
30
+ */
31
+ export declare function isSerializedRNG(object: unknown): object is SerializedRNG;
32
+ /**
33
+ * Helper function to initialize an RNG object using Blade's recommended shift index.
34
+ *
35
+ * @param seed The seed to initialize it with. Default is `getRandomSeed()`.
36
+ */
37
+ export declare function newRNG(seed?: Seed): RNG;
38
+ /**
39
+ * Helper function to get the next seed value.
40
+ *
41
+ * This function is useful if you are working with data structures that store seed values instead of
42
+ * RNG objects.
43
+ */
44
+ export declare function nextSeed(seed: Seed): Seed;
45
+ export {};