rpg-engine 0.17.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,7 @@
1
1
  import { type Page, type World } from "../../pages/index.ts";
2
+ import type { GameSystem } from "../../systems/index.ts";
2
3
  import { type Knowledge } from "../shared/characters/knowledge.ts";
4
+ import type { Measures } from "../shared/places/roads.ts";
3
5
  /** A page of the game system the campaign changed with a layer (SYS-11), as the dashboard lists it. */
4
6
  export interface ShownLayer {
5
7
  /** The page's name, by which its layer is taken off. */
@@ -8,7 +10,7 @@ export interface ShownLayer {
8
10
  title?: string;
9
11
  /** Its type, of a page the player may read. */
10
12
  type?: string;
11
- /** The fields the layer changes: each as the system gives it, none for one the layer adds, and as the campaign has it; none of a page the player may not read. */
13
+ /** The fields the layer changes: each as the system gives it, none for one the layer adds, and as the campaign has it, its measures in the story's units; none of a page the player may not read. */
12
14
  fields: {
13
15
  field: string;
14
16
  system?: string;
@@ -24,7 +26,7 @@ export interface ShownLayer {
24
26
  * value of every field; any other, as a creature the character does not know, by its name alone, never a value nor what changed: a layer is a house rule of
25
27
  * the campaign, not a fact of the story (D-565).
26
28
  */
27
- export declare function shownLayers(world: World, knowledge: Knowledge): ShownLayer[];
29
+ export declare function shownLayers(world: World, knowledge: Knowledge, system: GameSystem | undefined, measures: Measures): ShownLayer[];
28
30
  /** The page of the game system with a layer, by its name, as the view lists it and the player takes the layer off; undefined for any other. */
29
31
  export declare function listedLayer(world: World, name: string): Page | undefined;
30
32
  /**
@@ -1,26 +1,35 @@
1
1
  // The pages of the game system the campaign changed with a layer (SYS-11), as the player may see them: what changed, and what the system gave
2
+ import { lengthsInUnits } from "../../core/length.js";
2
3
  import { comparableText, sameName, valueNamed, valueText } from "../../core/text.js";
3
- import { baseChain, partContent, systemOriginal } from "../../pages/index.js";
4
+ import { baseChain, partContent, poundsInKilograms, systemOriginal } from "../../pages/index.js";
4
5
  import { inKnownWords, knowsPage } from "../shared/characters/knowledge.js";
5
6
  /**
6
7
  * The pages of the game system with a layer of the campaign, by name (SYS-11). One the player may read (VIEW-2) with what the layer changes and the system's
7
8
  * value of every field; any other, as a creature the character does not know, by its name alone, never a value nor what changed: a layer is a house rule of
8
9
  * the campaign, not a fact of the story (D-565).
9
10
  */
10
- export function shownLayers(world, knowledge) {
11
- const text = (value) => inKnownWords(world, knowledge, valueText(value));
11
+ export function shownLayers(world, knowledge, system, measures) {
12
+ const pound = measures.units === "metric" ? system?.pound : undefined;
13
+ const weightField = system?.load?.field;
14
+ // Measures in the story's units, as the sheet tells them (D-328, LOAD-2); a layer never holds aliases (INH-8), so no name is told in other units.
15
+ const text = (field, value) => {
16
+ const known = inKnownWords(world, knowledge, valueText(value));
17
+ return pound !== undefined && weightField !== undefined && comparableText(field) === comparableText(weightField)
18
+ ? poundsInKilograms(known, pound)
19
+ : lengthsInUnits(known, measures.units, measures.imperial);
20
+ };
12
21
  return layeredPages(world).map((page) => {
13
22
  const named = { name: page.name, ...(page.title === undefined ? {} : { title: page.title }) };
14
23
  if (!knowsPage(knowledge, page)) {
15
24
  return { ...named, fields: [], sections: [] };
16
25
  }
17
- const system = systemOriginal(page).fields;
26
+ const original = systemOriginal(page).fields;
18
27
  return {
19
28
  ...named,
20
29
  ...(page.type === undefined ? {} : { type: page.type }),
21
30
  fields: (page.layer?.fields ?? []).map((field) => {
22
- const given = valueNamed(system, field);
23
- return { field: sameName(Object.keys(system), field) ?? field, ...(given === undefined ? {} : { system: text(given) }), campaign: text(valueNamed(page.fields, field)) };
31
+ const given = valueNamed(original, field);
32
+ return { field: sameName(Object.keys(original), field) ?? field, ...(given === undefined ? {} : { system: text(field, given) }), campaign: text(field, valueNamed(page.fields, field)) };
24
33
  }),
25
34
  sections: (page.layer?.sections ?? []).filter((section) => partContent(page.body, section)?.secret === false),
26
35
  ...(page.layer?.introduction === true ? { introduction: true } : {}),
@@ -241,7 +241,7 @@ export const view = defineCommand({
241
241
  // The system's pages of the party's sheets, in the campaign's language where the system has no translation (D-539).
242
242
  wantTexts(root, context, world, [...players, ...companions]);
243
243
  const textsTranslating = translatingTexts(context, world);
244
- const layers = shownLayers(world, knowledge);
244
+ const layers = shownLayers(world, knowledge, system, measuresOf(root, system));
245
245
  return {
246
246
  kind: "overview",
247
247
  campaign: settings.name,
@@ -21,7 +21,10 @@ export declare function formatLength(metres: number, units: UnitSystem, imperial
21
21
  export declare function formatSpeed(metresAnHour: number, units: UnitSystem, imperial?: ImperialUnits): string;
22
22
  /** A short length, as on the map of a place (GRD-5), in the units of the story: 7.5 m; in imperial with the game's foot, 25 feet. */
23
23
  export declare function formatStep(metres: number, units: UnitSystem, imperial?: ImperialUnits): string;
24
- /** A text with the lengths it writes in feet told in the units of the story, as a speed of the sheet (D-328): 30 ft., fly 60 ft. is 9 m, fly 18 m in metric. */
24
+ /**
25
+ * A text with the lengths it writes in feet and miles told in the units of the story, with the game's foot and mile, as a speed of the sheet (D-328): 30 ft.,
26
+ * fly 60 ft. is 9 m, fly 18 m in metric; a 15-foot cone a 4.5 m cone, a 10-foot-radius sphere a 3 m radius sphere, 500 miles 750 km.
27
+ */
25
28
  export declare function lengthsInUnits(text: string, units: UnitSystem, imperial?: ImperialUnits): string;
26
29
  /** The first length a text gives, in metres, as a sheet writes a speed: 30 ft., 30 feet, 9 m; undefined when it gives none, or a number without its unit. */
27
30
  export declare function readFirstLength(value: unknown, imperial?: ImperialUnits): number | undefined;
@@ -35,9 +35,20 @@ export function formatSpeed(metresAnHour, units, imperial = trueImperialUnits) {
35
35
  export function formatStep(metres, units, imperial = trueImperialUnits) {
36
36
  return units === "imperial" ? `${Math.round(metres / imperial.foot)} feet` : `${oneDecimal(metres)} m`;
37
37
  }
38
- /** A text with the lengths it writes in feet told in the units of the story, as a speed of the sheet (D-328): 30 ft., fly 60 ft. is 9 m, fly 18 m in metric. */
38
+ /**
39
+ * A text with the lengths it writes in feet and miles told in the units of the story, with the game's foot and mile, as a speed of the sheet (D-328): 30 ft.,
40
+ * fly 60 ft. is 9 m, fly 18 m in metric; a 15-foot cone a 4.5 m cone, a 10-foot-radius sphere a 3 m radius sphere, 500 miles 750 km.
41
+ */
39
42
  export function lengthsInUnits(text, units, imperial = trueImperialUnits) {
40
- return units === "imperial" ? text : text.replace(/(\d+(?:\.\d+)?)\s*(?:ft\.?|feet|foot)(?![a-z])/gi, (_, number) => formatStep(Number(number) * imperial.foot, units, imperial));
43
+ if (units === "imperial") {
44
+ return text;
45
+ }
46
+ // A hyphen may join the number to its unit, and the unit to the word it measures.
47
+ return text.replace(/(\d{1,3}(?:,\d{3})+|\d+(?:\.\d+)?)(?:-|\s*)(ft\.?|feet|foot|miles?)(?![a-z])(-(?=[a-z]))?/gi, (_, number, unit, hyphen) => {
48
+ const amount = Number(number.replace(/,/g, ""));
49
+ const told = /^mile/i.test(unit) ? formatLength(amount * imperial.mile, units, imperial) : formatStep(amount * imperial.foot, units, imperial);
50
+ return hyphen === undefined ? told : `${told} `;
51
+ });
41
52
  }
42
53
  /** The first length a text gives, in metres, as a sheet writes a speed: 30 ft., 30 feet, 9 m; undefined when it gives none, or a number without its unit. */
43
54
  export function readFirstLength(value, imperial = trueImperialUnits) {
@@ -4,7 +4,7 @@ export { changedEnvironment, environmentAt, type EnvironmentEntry, formatEnviron
4
4
  export { changedFields, type FieldChange, formatFields, linkListItems, readFields, readFieldValue, splitPageFile } from "./file.ts";
5
5
  export { baseChain, baseFieldOf, baseValue, derivedPages, followersOf, holdsField, type Inherited, inheritedByBase, inheritedFields, introductionFollowersOf, isNeverInherited, keepersOf, newBaseProblem, ownField, ownFieldChange, removedFieldName, sameAsBase, sectionFollowersOf, shownPart, uniquePage, writtenFieldName, } from "./inheritance.ts";
6
6
  export { layeredPage, layeredPages, layerFolder, type LayerMark, layerParts, layersFolderName, layerTakes, systemOriginal } from "./layers.ts";
7
- export { carriedWeight, type LoadRule, weightUnit } from "./load.ts";
7
+ export { carriedWeight, type LoadRule, poundsInKilograms, weightUnit } from "./load.ts";
8
8
  export { type MapField, mapKey, mapOf, mapProblems, type PlaceMap, readMapField, sameFrame } from "./maps.ts";
9
9
  export { everyGoalTicked, firstTable, goalsIn, introductionText, type Link, links, partContent, readSection, secretNames, sectionTitles, type TextMode, textParts, withLinksAsWords, withoutComments, withoutFencedBlocks, withoutUnreadSecrets, } from "./markdown.ts";
10
10
  export { formatPosition, type Position, readPosition } from "./positions.ts";
@@ -5,7 +5,7 @@ export { changedEnvironment, environmentAt, formatEnvironmentEntry, placesAround
5
5
  export { changedFields, formatFields, linkListItems, readFields, readFieldValue, splitPageFile } from "./file.js";
6
6
  export { baseChain, baseFieldOf, baseValue, derivedPages, followersOf, holdsField, inheritedByBase, inheritedFields, introductionFollowersOf, isNeverInherited, keepersOf, newBaseProblem, ownField, ownFieldChange, removedFieldName, sameAsBase, sectionFollowersOf, shownPart, uniquePage, writtenFieldName, } from "./inheritance.js";
7
7
  export { layeredPage, layeredPages, layerFolder, layerParts, layersFolderName, layerTakes, systemOriginal } from "./layers.js";
8
- export { carriedWeight, weightUnit } from "./load.js";
8
+ export { carriedWeight, poundsInKilograms, weightUnit } from "./load.js";
9
9
  export { mapKey, mapOf, mapProblems, readMapField, sameFrame } from "./maps.js";
10
10
  export { everyGoalTicked, firstTable, goalsIn, introductionText, links, partContent, readSection, secretNames, sectionTitles, textParts, withLinksAsWords, withoutComments, withoutFencedBlocks, withoutUnreadSecrets, } from "./markdown.js";
11
11
  export { formatPosition, readPosition } from "./positions.js";
@@ -13,5 +13,7 @@ export interface LoadRule {
13
13
  export declare function carriedWeight(world: World, rule: LoadRule, holder: Page): number;
14
14
  /** The unit the system's weights are written in, as lb; undefined when no page says. */
15
15
  export declare function weightUnit(world: World, field: string): string | undefined;
16
+ /** A weight written in pounds told in kilograms at the system's pound, its note kept: 6 lb is 3 kg, 5 lb (full) 2.5 kg (full); any other text unchanged. */
17
+ export declare function poundsInKilograms(text: string, pound: number): string;
16
18
  /** How many pieces one of a kind holds, by the field the system declares for its bundle: 1 for a kind not sold by the bundle (STK-2). */
17
19
  export declare function bundleOf(field: string | undefined, kind: Page): number;
@@ -14,6 +14,15 @@ export function weightUnit(world, field) {
14
14
  const written = (world.system?.pages ?? []).map((page) => page.fields[field]).find((value) => typeof value === "string" && /\p{L}/u.test(value));
15
15
  return typeof written === "string" ? /(\p{L}[\p{L}.]*)\s*$/u.exec(written)?.[1] : undefined;
16
16
  }
17
+ /** A weight written in pounds told in kilograms at the system's pound, its note kept: 6 lb is 3 kg, 5 lb (full) 2.5 kg (full); any other text unchanged. */
18
+ export function poundsInKilograms(text, pound) {
19
+ const match = /^\s*([\d,]*(?:\.\d+)?)\s*(½|¼|¾|\d+\/\d+)?\s*(?:lbs?\.?|pounds?)\s*(\([^()]*\))?\s*$/iu.exec(text);
20
+ if (match === null || (match[1] === "" && match[2] === undefined)) {
21
+ return text;
22
+ }
23
+ const kilograms = Math.round(writtenWeight(match[1], match[2]) * pound * 1000) / 1000;
24
+ return match[3] === undefined ? `${kilograms} kg` : `${kilograms} kg ${match[3]}`;
25
+ }
17
26
  /** How many pieces one of a kind holds, by the field the system declares for its bundle: 1 for a kind not sold by the bundle (STK-2). */
18
27
  export function bundleOf(field, kind) {
19
28
  const pieces = field === undefined ? Number.NaN : Number(valueNamed(kind.fields, field));
@@ -56,10 +65,14 @@ function weightOf(world, field, page) {
56
65
  if (match === null || (match[1] === "" && match[2] === undefined)) {
57
66
  return 0;
58
67
  }
68
+ return writtenWeight(match[1], match[2]);
69
+ }
70
+ /** A weight's number from its whole part and its fraction, as written: 1,000, 0,5, 1 and ½, and 1/4. */
71
+ function writtenWeight(whole, fraction) {
59
72
  // A comma before three digits groups thousands, as 1,000; any other is a decimal comma, as 0,5.
60
- const whole = match[1] === "" ? 0 : Number(match[1].replace(/,(?=\d{3}(?!\d))/g, "").replace(",", "."));
61
- const part = match[2] === undefined ? 0 : { "½": 0.5, "¼": 0.25, "¾": 0.75 }[match[2]] ?? Number(match[2].split("/")[0]) / Number(match[2].split("/")[1]);
62
- return whole + part;
73
+ const number = whole === "" ? 0 : Number(whole.replace(/,(?=\d{3}(?!\d))/g, "").replace(",", "."));
74
+ const part = fraction === undefined ? 0 : { "½": 0.5, "¼": 0.25, "¾": 0.75 }[fraction] ?? Number(fraction.split("/")[0]) / Number(fraction.split("/")[1]);
75
+ return number + part;
63
76
  }
64
77
  /** What the coins of a purse weigh, as many coins as the system says make one unit of weight; 0 when it says none. */
65
78
  function coinsWeight(fields, perUnit) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rpg-engine",
3
- "version": "0.17.0",
3
+ "version": "0.17.1",
4
4
  "description": "Play a tabletop role-playing game with an AI as your game master: the AI tells the story; RPG Engine remembers the world, rolls the dice and keeps the accounts.",
5
5
  "type": "module",
6
6
  "bin": {