functions.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import { SugarCubeStoryVariables, SugarCubeTemporaryVariables } from "./userdata";
  2. declare global {
  3. /**
  4. * Returns a deep copy of the given value.
  5. *
  6. * @param original The object to value.
  7. *
  8. * NOTE: Only the primitives, generic objects, some JavaScript natives (specifically: Array, Date, Map, RegExp, and Set),
  9. * and DOM node objects are supported by default. Unsupported objects will need a .clone() method to be properly supported
  10. * by the cone() function—when called on such an object, it will simply defer to the local method.
  11. *
  12. * @since 2.0.0
  13. *
  14. * @example
  15. * // Without clone(); given the generic object: $foo = { id : 1 }
  16. * <<set $bar to $foo>>
  17. * <<set $bar.id to 5>>
  18. * $foo.id -> Returns: 5
  19. * $bar.id -> Returns: 5
  20. * // With clone(); given the generic object: $foo = { id : 1 }
  21. * <<set $bar to clone($foo)>>
  22. * <<set $bar.id to 5>>
  23. * $foo.id -> Returns: 1
  24. * $bar.id -> Returns: 5
  25. */
  26. function clone<T>(original: T): T;
  27. /**
  28. * Returns a random value from its given arguments.
  29. *
  30. * @param list The list of values to operate on. May be any combination of singular values, actual arrays, or array-like
  31. * objects. All values will be concatenated into a single list for selection. NOTE: Does not flatten nested arrays — if
  32. * this is required, the <Array>.flatten() method may be used to flatten the nested arrays prior to passing them to
  33. * either().
  34. * @since 2.0.0
  35. * @example
  36. * // Using singular values
  37. * either("Blueberry", "Cherry", "Pecan") -> Returns a random pie from the whole list
  38. *
  39. * // Using arrays; given: $pies = ["Blueberry", "Cherry", "Pecan"]
  40. * either($pies) -> Returns a random pie from the whole array
  41. *
  42. * // Using singular values and arrays; given: $letters = ["A", "B"]
  43. * either($letters, "C", "D") -> Returns a random value from the whole list (i.e. "A", "B", "C", "D")
  44. *
  45. * // Using multiple arrays; given: $letters = ["A", "B"] & $numerals = ["1", "2"]
  46. * either($letters, $numerals) -> Returns a random value from the whole list (i.e. "A", "B", "1", "2")
  47. */
  48. function either<T>(...list: readonly T[]): T;
  49. /**
  50. * Removes the specified key, and its associated value, from the story metadata store.
  51. * @param key The key to remove.
  52. * @since 2.29.0
  53. */
  54. function forget(key: string): void;
  55. /**
  56. * Returns whether the passage with the given title occurred within the story history. If multiple passage titles are given,
  57. * returns the logical-AND aggregate of the set (i.e. true if all were found, false if any were not found).
  58. * @param passageNames The title(s) of the passage(s) to search for. May be a list or an array of passages.
  59. * @since 2.7.0
  60. * @example
  61. * <<if hasVisited("Bar")>>…has been to the Bar…<</if>>
  62. * <<if not hasVisited("Bar")>>…has never been to the Bar…<</if>>
  63. * <<if hasVisited("Bar", "Café")>>…has been to both the Bar and Café<</if>>
  64. * <<if not hasVisited("Bar", "Café")>>…has never been to either the Bar, Café, or both…<</if>>
  65. */
  66. function hasVisited(...passageNames: readonly string[]): boolean;
  67. /**
  68. * Returns the number of turns that have passed since the last instance of the passage with the given title occurred within
  69. * the story history or -1 if it does not exist. If multiple passage titles are given, returns the lowest count (which can
  70. * be -1).
  71. * @param passageNames The title(s) of the passage(s) to search for. May be a list or an array of passages.
  72. * @since 2.0.0
  73. * @example
  74. * <<if lastVisited("Bar") is -1>>…has never been to the Bar…<</if>>
  75. * <<if lastVisited("Bar") is 0>>…is currently in the Bar…<</if>>
  76. * <<if lastVisited("Bar") is 1>>…was in the Bar one turn ago…<</if>>
  77. * <<if lastVisited("Bar", "Café") is -1>>…has never been to the Bar, Café, or both…<</if>>
  78. * <<if lastVisited("Bar", "Café") is 2>>…has been to both the Bar and Café, most recently two turns ago…<</if>>
  79. */
  80. function lastVisited(...passageNames: readonly string[]): number;
  81. /**
  82. * Load and integrate external JavaScript scripts.
  83. *
  84. * NOTE: Loading is done asynchronously at run time, so if the script must be available within a tight time frame, then you
  85. * should use the Promise returned by the function to ensure the script is loaded before before it is needed.
  86. *
  87. * NOTE: A script section (Twine 2: the Story JavaScript; Twine 1/Twee: a script-tagged passage) is normally the best place
  88. * to call importScripts().
  89. *
  90. * @param urls The URLs of the external scripts to import. Loose URLs are imported concurrently, arrays of URLs are imported
  91. * sequentially.
  92. *
  93. * @since 2.16.0
  94. *
  95. * @example Basic usage
  96. * // Import all scripts concurrently
  97. * importScripts(
  98. * "https://somesite/a/path/a.js",
  99. * "https://somesite/a/path/b.js",
  100. * "https://somesite/a/path/c.js",
  101. * "https://somesite/a/path/d.js"
  102. * );
  103. *
  104. * // Import all scripts sequentially
  105. * importScripts([
  106. * "https://somesite/a/path/a.js",
  107. * "https://somesite/a/path/b.js",
  108. * "https://somesite/a/path/c.js",
  109. * "https://somesite/a/path/d.js"
  110. * ]);
  111. *
  112. * // Import scripts a.js, b.js, and the c.js/d.js group concurrently,
  113. * // while importing c.js and d.js sequentially relative to each other
  114. * importScripts(
  115. * "https://somesite/a/path/a.js",
  116. * "https://somesite/a/path/b.js",
  117. * [
  118. * "https://somesite/a/path/c.js",
  119. * "https://somesite/a/path/d.js"
  120. * ]
  121. * );
  122. *
  123. * @example Basic usage with the returned Promise object
  124. * // Import a script while using the returned Promise to ensure that
  125. * // the script has been fully loaded before executing dependent code
  126. * importScripts("https://somesite/a/path/a.js")
  127. * .then(function () {
  128. * // Code that depends on the script goes here.
  129. * })
  130. * .catch(function (err) {
  131. * // There was an error loading the script, log it to the console.
  132. * console.log(err);
  133. * });
  134. *
  135. * @example <caption>Saving the returned Promise object for later use</caption>
  136. * // Import a script while saving the returned Promise so it may be used later
  137. * setup.aScriptImport = importScripts("https://somesite/a/path/aScript.js");
  138. *
  139. * // Use the returned Promise later on to ensure that the script has been fully
  140. * // loaded before executing dependent code
  141. * setup.aScriptImport
  142. * .then(function () {
  143. * // Code that depends on the script goes here.
  144. * })
  145. * .catch(function (err) {
  146. * // There was an error loading the script, log it to the console.
  147. * console.log(err);
  148. * });
  149. */
  150. function importScripts(...urls: readonly string[]): Promise<void>;
  151. /**
  152. * Load and integrate external CSS stylesheets.
  153. *
  154. * NOTE: Loading is done asynchronously at run time, so if the stylesheet must be available within a tight time frame, then
  155. * you should use the Promise returned by the function to ensure the stylesheet is loaded before it is needed.
  156. *
  157. * NOTE: A script section (Twine 2: the Story JavaScript; Twine 1/Twee: a script-tagged passage) is normally the best place
  158. * to call importStyles().
  159. * @param urls The URLs of the external stylesheets to import. Loose URLs are imported concurrently, arrays of URLs are imported sequentially.
  160. * @since 2.16.0
  161. * @example <caption>Basic usage</caption>
  162. * // Import all stylesheets concurrently
  163. * importStyles(
  164. * "https://somesite/a/path/a.css",
  165. * "https://somesite/a/path/b.css",
  166. * "https://somesite/a/path/c.css",
  167. * "https://somesite/a/path/d.css"
  168. * );
  169. *
  170. * // Import all stylesheets sequentially
  171. * importStyles([
  172. * "https://somesite/a/path/a.css",
  173. * "https://somesite/a/path/b.css",
  174. * "https://somesite/a/path/c.css",
  175. * "https://somesite/a/path/d.css"
  176. * ]);
  177. *
  178. * // Import stylesheets a.css, b.css, and the c.css/d.css group concurrently,
  179. * // while importing c.css and d.css sequentially relative to each other
  180. * importStyles(
  181. * "https://somesite/a/path/a.css",
  182. * "https://somesite/a/path/b.css",
  183. * [
  184. * "https://somesite/a/path/c.css",
  185. * "https://somesite/a/path/d.css"
  186. * ]
  187. * );
  188. *
  189. * @example <caption>Basic usage with the returned Promise object</caption>
  190. * // Grab a loading screen lock
  191. * var lsLockId = LoadScreen.lock();
  192. *
  193. * // Import a stylesheet while using the returned Promise to ensure that the
  194. * // stylesheet has been fully loaded before unlocking the loading screen
  195. * importStyles("https://somesite/a/path/a.css")
  196. * .then(function () {
  197. * // The stylesheet has been loaded, release the loading screen lock.
  198. * LoadScreen.unlock(lsLockId);
  199. * })
  200. * .catch(function (err) {
  201. * // There was an error loading the stylesheet, log it to the console.
  202. * console.log(err);
  203. * });
  204. */
  205. function importStyles(...urls: readonly string[]): Promise<void>;
  206. /**
  207. * Sets the specified key and value within the story metadata store, which causes them to persist over story and browser
  208. * restarts. To update the value associated with a key, simply set it again
  209. *
  210. * NOTE: The story metadata, like saves, is tied to the specific story it was generated with. It is not a mechanism for
  211. * moving data between stories.
  212. *
  213. * WARNING: The story metadata store is not, and should not be used as, a replacement for saves. Examples of good uses:
  214. * achievement tracking, new game+ data, playthrough statistics, etc.
  215. *
  216. * WARNING: This feature is largely incompatible with private browsing modes, which cause all in-browser storage mechanisms
  217. * to either persist only for the lifetime of the browsing session or fail outright.
  218. * @param key The key that should be set.
  219. * @param value The value to set.
  220. * @since 2.29.0
  221. * @example
  222. * // Sets 'achievements', with the given value, in the metadata store.
  223. * <<run memorize('achievements', { ateYellowSnow : true })>>
  224. *
  225. * // Sets 'ngplus', with the given value, in the metadata store.
  226. * <<run memorize('ngplus', true)>>
  227. */
  228. function memorize(key: string, value: any): void;
  229. /**
  230. * Returns the title of the active (present) passage.
  231. * @since 2.0.0
  232. * @example
  233. * <<if passage() is "Café">>…the current passage is the Café passage…<</if>>
  234. */
  235. function passage(): string;
  236. /**
  237. * Returns the title of the most recent previous passage whose title does not match that of the active passage or an empty
  238. * string, if there is no such passage.
  239. * @since 2.0.0
  240. * @example
  241. * <<if previous() is "Café">>…the most recent non-active passage is the Café passage…<</if>>
  242. * @example
  243. * // Commonly used as part of a link to return to the most recent non-active passage
  244. * [[Return|previous()]]
  245. */
  246. function previous(): string;
  247. /**
  248. * Returns a pseudo-random whole number (integer) within the range of the given bounds (inclusive)—i.e. [min, max].
  249. *
  250. * NOTE: By default, it uses Math.random() as its source of randomness, however, when the seedable PRNG has been enabled,
  251. * via State.initPRNG(), it uses the seeded PRNG instead.
  252. * @param min The lower bound of the random number (inclusive). If omitted, will default to 0.
  253. * @param max The upper bound of the random number (inclusive).
  254. * @since 2.0.0
  255. * @example
  256. * random(5) // Returns a number in the range 0–5
  257. * random(1, 6) // Returns a number in the range 1–6
  258. */
  259. function random(minOrMax: number, max?: number): number;
  260. /**
  261. * Returns a pseudo-random real number (floating-point) within the range of the given bounds (inclusive for the minimum,
  262. * exclusive for the maximum) — i.e. [min, max).
  263. *
  264. * NOTE: By default, it uses Math.random() as its source of randomness, however, when the seedable PRNG has been enabled,
  265. * via State.initPRNG(), it uses the seeded PRNG instead.
  266. * @param min The lower bound of the random number (inclusive). If omitted, will default to 0.0.
  267. * @param max The upper bound of the random number (exclusive).
  268. * @since 2.0.0
  269. * @example
  270. * randomFloat(5.0) // Returns a number in the range 0.0–4.9999999…
  271. * randomFloat(1.0, 6.0) // Returns a number in the range 1.0–5.9999999…
  272. */
  273. function randomFloat(minOrMax: number, max?: number): number;
  274. /**
  275. * Returns the value associated with the specified key from the story metadata store or, if no such key exists, the specified
  276. * default value, if any.
  277. * @param key The key whose value should be returned.
  278. * @param defaultValue The value to return if the key doesn't exist.
  279. * @since 2.29.0
  280. * @example
  281. * // Set setup.achievements to the 'achievements' metadata or an empty generic object.
  282. * <<set setup.achievements to recall('achievements', {})>>
  283. *
  284. * // Set setup.ngplus to the 'ngplus' metadata, with no default.
  285. * <<set setup.ngplus to recall('ngplus')>>
  286. */
  287. function recall(key: string, defaultValue?: any): any;
  288. /**
  289. * Renders the selected passage into the target element, replacing any existing content, and returns the element. If no passages are found and default text is specified, it will be used instead.
  290. * @param idOrElement The ID of the element or the element itself.
  291. * @param passages The name(s) of the passage(s) to search for. May be a single passage or an array of passages. If an array
  292. * of passage names is specified, the first passage to be found is used.
  293. * @param defaultText The default text to use if no passages are found.
  294. *
  295. * @since 2.0.0
  296. *
  297. * NOTE: As it is highly unlikely that either an array of passage names or default text will be needed in the vast majority
  298. * of cases, only a few basic examples will be given.
  299. * @example
  300. * // Using an ID; given an existing element on the page: <div id="my-display"></div>
  301. * setPageElement("my-display", "MyPassage");
  302. * @example
  303. * // Using an element; given a reference to an existing element: myElement
  304. * setPageElement(myElement, "MyPassage");
  305. */
  306. function setPageElement(
  307. idOrElement: string | HTMLElement,
  308. passages: string | string[],
  309. defaultText?: string,
  310. ): HTMLElement | null;
  311. /**
  312. * Returns a new array consisting of all of the tags of the given passages.
  313. * @param passages The passages from which to collect tags. May be a list or an array of passages. If omitted, will default
  314. * to the current passage.
  315. * @since 2.0.0
  316. * @example
  317. * <<if tags().includes("forest")>>…the current passage is part of the forest…<</if>>
  318. * <<if tags("Lonely Glade").includes("forest")>>…the Lonely Glade passage is part of the forest…<</if>>
  319. */
  320. function tags(...passages: readonly string[]): string[];
  321. /**
  322. * Returns a reference to the current temporary variables store (equivalent to: State.temporary). This is only really useful
  323. * within pure JavaScript code, as within TwineScript you may simply access temporary variables natively.
  324. * @since 2.19.0
  325. * @example
  326. * // Given: _selection is 'Zagnut Bar'
  327. * if (temporary().selection === 'Zagnut Bar') {
  328. * // Do something...
  329. * }
  330. */
  331. function temporary(): SugarCubeTemporaryVariables;
  332. /**
  333. * Returns the number of milliseconds which have passed since the current passage was rendered to the page.
  334. * @since 2.0.0
  335. * @example
  336. * // Links which vary based on the time
  337. * In the darkness, something wicked this way comes. Quickly! Do you \
  338. * <<link "try to run back into the light">>
  339. * <<if time() lt 5000>>
  340. * /% The player clicked the link in under 5s, so they escape %/
  341. * <<goto "Well lit passageway">>
  342. * <<else>>
  343. * /% Else, they're eaten by a grue %/
  344. * <<goto "Eaten by a grue">>
  345. * <</if>>
  346. * <</link>> \
  347. * or [[stand your ground|Eaten by a grue]]?
  348. */
  349. function time(): number;
  350. /**
  351. * Returns the number of passages that the player has visited.
  352. * @since 2.0.0
  353. * @example
  354. * << print "This is turn #" + turns() >>
  355. */
  356. function turns(): number;
  357. /**
  358. * Returns a reference to the active(present) story variables store(equivalent to: State.variables).This is only really
  359. * useful within pure JavaScript code, as within TwineScript you may simply access story variables natively.
  360. * @since 2.0.0
  361. * @example
  362. * // Given: $hasGoldenKey is true
  363. * if (variables().hasGoldenKey) {
  364. * //Do something
  365. * }
  366. */
  367. function variables(): SugarCubeStoryVariables;
  368. /**
  369. * Returns the number of times that the passage with the given title occurred within the story history. If multiple passage
  370. * titles are given, returns the lowest count.
  371. * @param passages The title(s) of the passage(s) to search for. May be a list or an array of passages. If omitted, will
  372. * default to the current passage.
  373. * @since 2.0.0
  374. * @example
  375. * <<if visited() is 3>>…this is the third visit to the current passage…<</if>>
  376. * <<if visited("Bar")>>…has been to the Bar at least once…<</if>>
  377. * <<if visited("Café") is 1>>…has been to the Café exactly once…<</if>>
  378. * <<if visited("Bar", "Café") is 4>>…has been to both the Bar and Café at least four times…<</if>>
  379. */
  380. function visited(...passages: readonly string[]): number;
  381. /**
  382. * Returns the number of passages within the story history which are tagged with all of the given tags.
  383. * @param tags The tags to search for. May be a list or an array of tags.
  384. * @since 2.0.0
  385. * @example
  386. * <<if visitedTags("forest")>>…has been to some part of the forest at least once…<</if>>
  387. * <<if visitedTags("forest", "haunted") is 1>>…has been to the haunted part of the forest exactly once…<</if>>
  388. * <<if visitedTags("forest", "burned") is 3>>…has been to the burned part of the forest three times…<</if>>
  389. */
  390. function visitedTags(...tags: readonly string[]): number;
  391. }
  392. export {};