@zergai/ztc 0.26.6-dev.202611240947 → 0.27.0-beta.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.
Files changed (268) hide show
  1. package/README.md +629 -167
  2. package/RUST_THIRD_PARTY_LICENSES.txt +5 -5
  3. package/bin/zmux-darwin-arm64 +0 -0
  4. package/bin/zmux-linux-x64 +0 -0
  5. package/bin/ztc-audio-darwin-arm64 +0 -0
  6. package/bin/ztc-capture-darwin-arm64 +0 -0
  7. package/bin/ztc-computer-darwin-arm64 +0 -0
  8. package/bin/ztc-llm-darwin-arm64 +0 -0
  9. package/bin/ztc-tts-darwin-arm64 +0 -0
  10. package/bin/ztc-tui-darwin-arm64 +0 -0
  11. package/bin/ztc-tui-linux-x64 +0 -0
  12. package/bin/zterm-darwin-arm64 +2 -2
  13. package/dist/App.js +6 -1
  14. package/dist/agent/agent.d.ts +21 -15
  15. package/dist/agent/agent.js +389 -163
  16. package/dist/agent/backends/codex_app_server.d.ts +7 -1
  17. package/dist/agent/backends/codex_app_server.js +11 -1
  18. package/dist/agent/backends/gemini.js +22 -8
  19. package/dist/agent/commands/batch.js +8 -0
  20. package/dist/agent/commands/config.js +10 -0
  21. package/dist/agent/commands/environment.js +5 -5
  22. package/dist/agent/commands/goal.d.ts +1 -0
  23. package/dist/agent/commands/goal.js +2 -1
  24. package/dist/agent/commands/index.js +2 -1
  25. package/dist/agent/commands/manifest.js +4 -0
  26. package/dist/agent/commands/optimize.js +5 -2
  27. package/dist/agent/commands/pane_cli.d.ts +29 -0
  28. package/dist/agent/commands/pane_cli.js +303 -0
  29. package/dist/agent/commands/settings.js +13 -3
  30. package/dist/agent/commands/shannon.js +8 -0
  31. package/dist/agent/commands/update.js +15 -3
  32. package/dist/agent/commands/zerg.js +31 -1
  33. package/dist/agent/commands/ztc_pane.js +3 -1
  34. package/dist/agent/factory.d.ts +4 -0
  35. package/dist/agent/factory.js +11 -1
  36. package/dist/agent/loop_policy.d.ts +42 -5
  37. package/dist/agent/loop_policy.js +206 -3
  38. package/dist/agent/runtime/command_jobs.d.ts +5 -0
  39. package/dist/agent/runtime/command_jobs.js +241 -31
  40. package/dist/agent/runtime/command_wait.d.ts +2 -0
  41. package/dist/agent/runtime/command_wait.js +1 -0
  42. package/dist/agent/runtime/done_report.d.ts +18 -0
  43. package/dist/agent/runtime/done_report.js +130 -0
  44. package/dist/agent/runtime/execution_context.js +4 -1
  45. package/dist/agent/runtime/failure_cause.d.ts +2 -0
  46. package/dist/agent/runtime/failure_cause.js +35 -0
  47. package/dist/agent/runtime/policy.d.ts +14 -2
  48. package/dist/agent/runtime/policy.js +104 -12
  49. package/dist/agent/runtime/quiet_job.d.ts +18 -0
  50. package/dist/agent/runtime/quiet_job.js +48 -0
  51. package/dist/agent/runtime/recovery.d.ts +1 -5
  52. package/dist/agent/runtime/recovery.js +2 -44
  53. package/dist/agent/runtime/tool_runtime.d.ts +1 -0
  54. package/dist/agent/runtime/tool_runtime.js +2 -0
  55. package/dist/agent/subagent.js +6 -1
  56. package/dist/agent/tools/deferred.js +1 -0
  57. package/dist/agent/tools/index.d.ts +2 -0
  58. package/dist/agent/tools/index.js +1 -0
  59. package/dist/agent/tools/outcome.d.ts +15 -0
  60. package/dist/agent/tools/outcome.js +107 -0
  61. package/dist/agent/tools/pane.d.ts +2 -1
  62. package/dist/agent/tools/pane.js +21 -3
  63. package/dist/agent/tools/registry.js +2 -0
  64. package/dist/agent/tools/shell.d.ts +1 -0
  65. package/dist/agent/tools/shell.js +128 -17
  66. package/dist/agent/tools/sim_domain_tools.js +4 -2
  67. package/dist/agent/tools/supervision.js +4 -2
  68. package/dist/agent/tools/types.d.ts +3 -0
  69. package/dist/app/command_context.js +5 -9
  70. package/dist/app/conversation_compaction.d.ts +4 -2
  71. package/dist/app/conversation_compaction.js +16 -5
  72. package/dist/app/headless_shell_mode.d.ts +14 -0
  73. package/dist/app/headless_shell_mode.js +7 -0
  74. package/dist/app/hosted_resident.d.ts +356 -0
  75. package/dist/app/hosted_resident.js +677 -0
  76. package/dist/app/restart.js +11 -2
  77. package/dist/app/session_manager.d.ts +5 -0
  78. package/dist/app/session_manager.js +21 -6
  79. package/dist/app/session_messages.js +6 -6
  80. package/dist/app/workspace_layout_controller.js +7 -0
  81. package/dist/batch/worktree.d.ts +10 -0
  82. package/dist/batch/worktree.js +15 -0
  83. package/dist/bench/registry.js +4 -2
  84. package/dist/cli_args.js +1 -1
  85. package/dist/cli_main.js +80 -10
  86. package/dist/cli_ratatui.d.ts +75 -0
  87. package/dist/cli_ratatui.js +485 -84
  88. package/dist/components/InputArea.js +4 -4
  89. package/dist/config/execution.d.ts +10 -0
  90. package/dist/config/execution.js +10 -0
  91. package/dist/config/hosted_runtime.d.ts +4 -1
  92. package/dist/config/hosted_runtime.js +4 -1
  93. package/dist/config/permission_pin.d.ts +34 -2
  94. package/dist/config/permission_pin.js +52 -2
  95. package/dist/config/person_command_policy.d.ts +38 -0
  96. package/dist/config/person_command_policy.js +11 -0
  97. package/dist/config/profiles.d.ts +21 -0
  98. package/dist/config/profiles.js +56 -2
  99. package/dist/config/types.d.ts +4 -1
  100. package/dist/config.js +34 -19
  101. package/dist/desktop/daily/codex_exec_adapter.js +7 -2
  102. package/dist/desktop/daily/desktop_wiring.d.ts +7 -1
  103. package/dist/desktop/daily/desktop_wiring.js +11 -3
  104. package/dist/desktop/daily/recipes.js +20 -4
  105. package/dist/desktop/health.d.ts +24 -0
  106. package/dist/desktop/health.js +151 -0
  107. package/dist/desktop/native_dialogs.d.ts +37 -0
  108. package/dist/desktop/native_dialogs.js +105 -0
  109. package/dist/diagnostics/session_export.d.ts +1 -0
  110. package/dist/diagnostics/session_export.js +28 -0
  111. package/dist/embedded/registry.js +4 -2
  112. package/dist/emulation/tool_adapters.js +9 -4
  113. package/dist/emulation/ztc_addendum.js +3 -0
  114. package/dist/headless.d.ts +11 -4
  115. package/dist/headless.js +99 -34
  116. package/dist/headless_approval.d.ts +6 -0
  117. package/dist/headless_cli.js +1 -1
  118. package/dist/hooks/runner.js +7 -6
  119. package/dist/library.d.ts +4 -0
  120. package/dist/library.js +3 -0
  121. package/dist/mcp/client.js +3 -3
  122. package/dist/modes/registry.js +13 -4
  123. package/dist/onboarding/setup_environment.js +13 -1
  124. package/dist/optimize/adapters/kernelbench.js +88 -10
  125. package/dist/optimize/adapters/types.d.ts +5 -0
  126. package/dist/optimize/executor.js +21 -0
  127. package/dist/panes/cli_install.d.ts +54 -0
  128. package/dist/panes/cli_install.js +157 -0
  129. package/dist/panes/model_catalog.js +3 -0
  130. package/dist/panes/new_pane_choices.d.ts +6 -2
  131. package/dist/panes/new_pane_choices.js +24 -12
  132. package/dist/panes/pane_cli_launch.d.ts +50 -0
  133. package/dist/panes/pane_cli_launch.js +124 -0
  134. package/dist/panes/pane_clis.d.ts +171 -0
  135. package/dist/panes/pane_clis.js +325 -0
  136. package/dist/panes/person_only_panes.d.ts +45 -0
  137. package/dist/panes/person_only_panes.js +117 -0
  138. package/dist/plugin/simulation_living.js +0 -2
  139. package/dist/plugin/simulation_living_engine.d.ts +1 -0
  140. package/dist/plugin/simulation_living_engine.js +1 -0
  141. package/dist/plugin/simulation_living_labels.d.ts +3 -0
  142. package/dist/plugin/simulation_living_labels.js +2 -0
  143. package/dist/router_auth.js +12 -1
  144. package/dist/session.js +26 -1
  145. package/dist/simulation/art/gemini_image.js +11 -5
  146. package/dist/simulation/domains/loader.js +25 -3
  147. package/dist/simulation/living/cold.d.ts +23 -0
  148. package/dist/simulation/living/cold.js +41 -0
  149. package/dist/simulation/living/context.d.ts +591 -1283
  150. package/dist/simulation/living/context.js +378 -244
  151. package/dist/simulation/living/contradictions.d.ts +14 -0
  152. package/dist/simulation/living/contradictions.js +282 -0
  153. package/dist/simulation/living/core/ids.d.ts +53 -0
  154. package/dist/simulation/living/core/ids.js +39 -0
  155. package/dist/simulation/living/core/kind.d.ts +20 -0
  156. package/dist/simulation/living/core/kind.js +15 -0
  157. package/dist/simulation/living/core/laws.d.ts +179 -0
  158. package/dist/simulation/living/core/laws.js +184 -0
  159. package/dist/simulation/living/core/ops.d.ts +257 -0
  160. package/dist/simulation/living/core/ops.js +34 -0
  161. package/dist/simulation/living/definer.d.ts +17 -1
  162. package/dist/simulation/living/definer.js +130 -51
  163. package/dist/simulation/living/definition.d.ts +174 -65
  164. package/dist/simulation/living/definition.js +287 -180
  165. package/dist/simulation/living/director.d.ts +80 -0
  166. package/dist/simulation/living/director.js +363 -0
  167. package/dist/simulation/living/frame.d.ts +108 -21
  168. package/dist/simulation/living/frame.js +111 -66
  169. package/dist/simulation/living/identifiers.d.ts +0 -1
  170. package/dist/simulation/living/identifiers.js +0 -3
  171. package/dist/simulation/living/index.d.ts +18 -9
  172. package/dist/simulation/living/index.js +18 -9
  173. package/dist/simulation/living/inference.d.ts +92 -50
  174. package/dist/simulation/living/inference.js +319 -175
  175. package/dist/simulation/living/knowledge.d.ts +28 -0
  176. package/dist/simulation/living/knowledge.js +73 -0
  177. package/dist/simulation/living/library.json +1177 -0
  178. package/dist/simulation/living/mechanics.d.ts +31 -34
  179. package/dist/simulation/living/mechanics.js +222 -138
  180. package/dist/simulation/living/model-input.d.ts +6 -3
  181. package/dist/simulation/living/model-input.js +18 -26
  182. package/dist/simulation/living/open-world.d.ts +74 -94
  183. package/dist/simulation/living/open-world.js +151 -30
  184. package/dist/simulation/living/phrases.d.ts +3 -0
  185. package/dist/simulation/living/phrases.js +37 -0
  186. package/dist/simulation/living/phrases.json +560 -0
  187. package/dist/simulation/living/projection.d.ts +316 -2
  188. package/dist/simulation/living/projection.js +50 -2
  189. package/dist/simulation/living/prompts/example-spec.json +11 -0
  190. package/dist/simulation/living/prompts/role-text.json +14 -0
  191. package/dist/simulation/living/prompts.d.ts +5 -0
  192. package/dist/simulation/living/prompts.js +275 -0
  193. package/dist/simulation/living/recall.d.ts +2 -2
  194. package/dist/simulation/living/reduce.d.ts +51 -14
  195. package/dist/simulation/living/reduce.js +941 -579
  196. package/dist/simulation/living/replay.d.ts +5 -4
  197. package/dist/simulation/living/replay.js +10 -9
  198. package/dist/simulation/living/runtime.d.ts +135 -25
  199. package/dist/simulation/living/runtime.js +1641 -583
  200. package/dist/simulation/living/scale.d.ts +96 -0
  201. package/dist/simulation/living/scale.js +124 -0
  202. package/dist/simulation/living/script.d.ts +5 -0
  203. package/dist/simulation/living/script.js +115 -0
  204. package/dist/simulation/living/shared-kinds.d.ts +4 -11
  205. package/dist/simulation/living/shared-kinds.js +11 -81
  206. package/dist/simulation/living/space.d.ts +14 -0
  207. package/dist/simulation/living/space.js +132 -0
  208. package/dist/simulation/living/state.d.ts +100 -0
  209. package/dist/simulation/living/state.js +393 -0
  210. package/dist/simulation/living/types.d.ts +829 -512
  211. package/dist/simulation/living/types.js +162 -187
  212. package/dist/simulation/living/world-rules.d.ts +100 -0
  213. package/dist/simulation/living/world-rules.js +267 -0
  214. package/dist/simulation/plugins/loader.js +11 -0
  215. package/dist/skills/loader.js +4 -0
  216. package/dist/tools.d.ts +1 -0
  217. package/dist/tools.js +1 -0
  218. package/dist/ui/ratatui/headless_pane_manager.d.ts +4 -1
  219. package/dist/ui/ratatui/headless_pane_manager.js +13 -9
  220. package/dist/ui/ratatui/pane_relay.js +29 -0
  221. package/dist/ui/ratatui/pane_type.d.ts +10 -0
  222. package/dist/ui/ratatui/pane_type.js +11 -0
  223. package/dist/ui/ratatui/process.d.ts +30 -0
  224. package/dist/ui/ratatui/process.js +133 -19
  225. package/dist/ui/ratatui/protocol.d.ts +124 -2
  226. package/dist/ui/ratatui/protocol.js +35 -0
  227. package/dist/ui/ratatui/relay_policy.js +3 -0
  228. package/dist/ui/ratatui/ztc_launch_trust.d.ts +2 -1
  229. package/dist/ui/ratatui/ztc_launch_trust.js +8 -1
  230. package/dist/ui/terminal_image.d.ts +12 -0
  231. package/dist/ui/terminal_image.js +21 -1
  232. package/dist/utils/codex_project_trust.d.ts +19 -0
  233. package/dist/utils/codex_project_trust.js +37 -0
  234. package/dist/utils/compaction.js +103 -20
  235. package/dist/utils/git_program_files.d.ts +19 -12
  236. package/dist/utils/git_program_files.js +411 -83
  237. package/dist/utils/host_shell.d.ts +10 -1
  238. package/dist/utils/host_shell.js +14 -1
  239. package/dist/utils/mid_turn_compaction.d.ts +2 -0
  240. package/dist/utils/mid_turn_compaction.js +2 -0
  241. package/dist/utils/models.js +6 -1
  242. package/dist/utils/sandbox.d.ts +109 -8
  243. package/dist/utils/sandbox.js +981 -187
  244. package/dist/utils/shell.d.ts +2 -0
  245. package/dist/utils/shell.js +10 -4
  246. package/dist/utils/subprocess_env.d.ts +47 -11
  247. package/dist/utils/subprocess_env.js +107 -15
  248. package/dist/utils/system_ps.d.ts +8 -0
  249. package/dist/utils/system_ps.js +39 -7
  250. package/dist/utils/unattended_compaction.d.ts +9 -0
  251. package/dist/utils/unattended_compaction.js +13 -0
  252. package/dist/utils/ztc_own_state.d.ts +8 -5
  253. package/dist/utils/ztc_own_state.js +17 -11
  254. package/dist/web/mirror_client.js +1 -0
  255. package/dist/web/mirror_stream.d.ts +50 -6
  256. package/dist/web/mirror_stream.js +160 -15
  257. package/dist/workflows/registry.js +3 -1
  258. package/dist/workspace/controller.js +12 -0
  259. package/dist/workspace_collision.d.ts +3 -0
  260. package/dist/workspace_collision.js +14 -1
  261. package/dist/workspace_session_context.js +2 -1
  262. package/dist/zlm-embed.mjs +3823 -1970
  263. package/node_modules/@zerg/model-contracts/README.md +16 -4
  264. package/node_modules/@zerg/model-contracts/index.js +6 -1
  265. package/npm-shrinkwrap.json +1 -1
  266. package/package.json +42 -24
  267. package/packages/model-contracts/README.md +16 -4
  268. package/packages/model-contracts/index.js +6 -1
@@ -0,0 +1,96 @@
1
+ import { z } from 'zod';
2
+ export declare const restWindowSchema: z.ZodObject<{
3
+ from: z.ZodNumber;
4
+ until: z.ZodNumber;
5
+ span: z.ZodOptional<z.ZodNumber>;
6
+ action: z.ZodString;
7
+ why: z.ZodString;
8
+ }, z.core.$strip>;
9
+ export declare const calendarSchema: z.ZodObject<{
10
+ beat: z.ZodNumber;
11
+ day: z.ZodNumber;
12
+ hours: z.ZodDefault<z.ZodNumber>;
13
+ minutes: z.ZodDefault<z.ZodNumber>;
14
+ rest: z.ZodDefault<z.ZodNullable<z.ZodObject<{
15
+ from: z.ZodNumber;
16
+ until: z.ZodNumber;
17
+ span: z.ZodOptional<z.ZodNumber>;
18
+ action: z.ZodString;
19
+ why: z.ZodString;
20
+ }, z.core.$strip>>>;
21
+ }, z.core.$strip>;
22
+ export type LivingCalendar = z.infer<typeof calendarSchema>;
23
+ export type LivingRestWindow = z.infer<typeof restWindowSchema>;
24
+ export declare const DEFAULT_CALENDAR: LivingCalendar;
25
+ export declare function clockLabel(calendar: LivingCalendar, at: number): string;
26
+ export declare const MAX_BOUND = 1000000000;
27
+ declare const PACING_VALUES: {
28
+ readonly exchangeLive: 10;
29
+ readonly exchangeTurn: 30;
30
+ readonly exchangeCheck: 5;
31
+ readonly askDecision: 30;
32
+ readonly contactAnswer: 20;
33
+ readonly incidentalActivity: 30;
34
+ readonly departureReady: 10;
35
+ readonly promiseLead: 30;
36
+ readonly stall: 120;
37
+ readonly longDay: 960;
38
+ readonly reminder: 120;
39
+ readonly playerQuiet: 120;
40
+ readonly unanswered: 10;
41
+ readonly unansweredReset: 480;
42
+ readonly pressing: 240;
43
+ readonly nonPlayerBudget: 20;
44
+ readonly shockPlanDelay: 5;
45
+ readonly shockSpacing: 5;
46
+ readonly aftermathSoon: 15;
47
+ readonly aftermathLater: 45;
48
+ readonly aftermathGap: 30;
49
+ readonly othersMax: 10;
50
+ readonly longAct: 10;
51
+ readonly deadlineGrace: 15;
52
+ readonly errandDelay: 3;
53
+ readonly speech: 1;
54
+ readonly message: 0;
55
+ readonly messageReplyDelay: 10;
56
+ readonly idleWait: 60;
57
+ readonly overdueWait: 15;
58
+ readonly defaultActivity: 30;
59
+ readonly carryOnLimit: 120;
60
+ readonly directorCheck: 30;
61
+ readonly directorGap: 45;
62
+ readonly directorQuiet: 90;
63
+ readonly directorSerious: 20;
64
+ readonly directorLead: 30;
65
+ readonly actDefault: 5;
66
+ readonly offstageMin: 15;
67
+ readonly reactionBeat: 1;
68
+ readonly sessionReach: 90;
69
+ };
70
+ export type PacingKey = keyof typeof PACING_VALUES;
71
+ export declare const PACING: Readonly<Record<PacingKey, number>>;
72
+ declare const REACTOR_STAKE_VALUES: {
73
+ readonly touchesPerson: 8;
74
+ readonly touchesThing: 6;
75
+ readonly relatesToPerson: 4;
76
+ readonly planTarget: 3;
77
+ readonly openRequest: 3;
78
+ readonly openTension: 2;
79
+ };
80
+ export type ReactorStakeKey = keyof typeof REACTOR_STAKE_VALUES;
81
+ export declare const REACTOR_STAKE: Readonly<Record<ReactorStakeKey, number>>;
82
+ export declare function pace(calendar: LivingCalendar, key: PacingKey): number;
83
+ export declare function beats(calendar: LivingCalendar, count: number): number;
84
+ export declare function restSpan(calendar: LivingCalendar): number;
85
+ export declare function restingAt(calendar: LivingCalendar, at: number): boolean;
86
+ export declare function offstageSpan(calendar: LivingCalendar): number;
87
+ export declare function restActionOf(calendar: LivingCalendar): string | null;
88
+ export type TravelState = {
89
+ calendar: LivingCalendar;
90
+ travelSteps: number;
91
+ places: Record<string, {
92
+ edgeCosts?: Record<string, number>;
93
+ }>;
94
+ };
95
+ export declare function travelCost(state: TravelState, path: readonly string[]): number;
96
+ export {};
@@ -0,0 +1,124 @@
1
+ import { z } from 'zod';
2
+ import { idSchema } from './open-world.js';
3
+ import { phrase } from './phrases.js';
4
+ export const restWindowSchema = z.object({
5
+ from: z.number().int().min(0).describe(phrase('calendar.rest.from')),
6
+ until: z.number().int().min(0).describe(phrase('calendar.rest.until')),
7
+ span: z.number().int().min(1).max(1_000_000_000).optional().describe(phrase('calendar.rest.span')),
8
+ action: idSchema.describe(phrase('calendar.rest.action')),
9
+ why: z.string().min(1).max(200).describe(phrase('calendar.rest.why')),
10
+ });
11
+ export const calendarSchema = z.object({
12
+ beat: z.number().int().min(1).max(1_000_000).describe(phrase('calendar.beat')),
13
+ day: z.number().int().min(1).max(1_000_000_000).describe(phrase('calendar.day')),
14
+ hours: z.number().int().min(1).max(10_000).default(24).describe(phrase('calendar.hours')),
15
+ minutes: z.number().int().min(1).max(10_000).default(60).describe(phrase('calendar.minutes')),
16
+ rest: restWindowSchema.nullable().default(null).describe(phrase('schema.description.181')),
17
+ });
18
+ export const DEFAULT_CALENDAR = { beat: 1, day: 1440, hours: 24, minutes: 60, rest: null };
19
+ export function clockLabel(calendar, at) {
20
+ const day = Math.floor(at / calendar.day) + 1;
21
+ const within = ((at % calendar.day) + calendar.day) % calendar.day;
22
+ const total = Math.floor((within * calendar.hours * calendar.minutes) / calendar.day);
23
+ const hour = Math.floor(total / calendar.minutes);
24
+ const minute = total % calendar.minutes;
25
+ return phrase('clock.label', { day, clock: `${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}` });
26
+ }
27
+ export const MAX_BOUND = 1_000_000_000;
28
+ const PACING_VALUES = {
29
+ exchangeLive: 10,
30
+ exchangeTurn: 30,
31
+ exchangeCheck: 5,
32
+ askDecision: 30,
33
+ contactAnswer: 20,
34
+ incidentalActivity: 30,
35
+ departureReady: 10,
36
+ promiseLead: 30,
37
+ stall: 120,
38
+ longDay: 960,
39
+ reminder: 120,
40
+ playerQuiet: 120,
41
+ unanswered: 10,
42
+ unansweredReset: 480,
43
+ pressing: 240,
44
+ nonPlayerBudget: 20,
45
+ shockPlanDelay: 5,
46
+ shockSpacing: 5,
47
+ aftermathSoon: 15,
48
+ aftermathLater: 45,
49
+ aftermathGap: 30,
50
+ othersMax: 10,
51
+ longAct: 10,
52
+ deadlineGrace: 15,
53
+ errandDelay: 3,
54
+ speech: 1,
55
+ message: 0,
56
+ messageReplyDelay: 10,
57
+ idleWait: 60,
58
+ overdueWait: 15,
59
+ defaultActivity: 30,
60
+ carryOnLimit: 120,
61
+ directorCheck: 30,
62
+ directorGap: 45,
63
+ directorQuiet: 90,
64
+ directorSerious: 20,
65
+ directorLead: 30,
66
+ actDefault: 5,
67
+ offstageMin: 15,
68
+ reactionBeat: 1,
69
+ sessionReach: 90,
70
+ };
71
+ export const PACING = Object.freeze(PACING_VALUES);
72
+ const REACTOR_STAKE_VALUES = {
73
+ touchesPerson: 8,
74
+ touchesThing: 6,
75
+ relatesToPerson: 4,
76
+ planTarget: 3,
77
+ openRequest: 3,
78
+ openTension: 2,
79
+ };
80
+ export const REACTOR_STAKE = Object.freeze(REACTOR_STAKE_VALUES);
81
+ const ANSWER_PACING = Object.freeze({
82
+ askDecision: true,
83
+ contactAnswer: true,
84
+ messageReplyDelay: true,
85
+ });
86
+ export function pace(calendar, key) {
87
+ if (ANSWER_PACING[key] === true)
88
+ return calendar.beat;
89
+ return PACING[key] * calendar.beat;
90
+ }
91
+ export function beats(calendar, count) {
92
+ return count * calendar.beat;
93
+ }
94
+ export function restSpan(calendar) {
95
+ const rest = calendar.rest;
96
+ if (rest === null)
97
+ return calendar.day;
98
+ if (rest.span !== undefined)
99
+ return rest.span;
100
+ const span = (rest.until - rest.from + calendar.day) % calendar.day;
101
+ return span === 0 ? calendar.day : span;
102
+ }
103
+ export function restingAt(calendar, at) {
104
+ const rest = calendar.rest;
105
+ if (rest === null)
106
+ return false;
107
+ const within = ((at % calendar.day) + calendar.day) % calendar.day;
108
+ return rest.from <= rest.until ? within >= rest.from && within < rest.until : within >= rest.from || within < rest.until;
109
+ }
110
+ export function offstageSpan(calendar) {
111
+ return Math.max(calendar.beat, Math.floor(calendar.day / 12));
112
+ }
113
+ export function restActionOf(calendar) {
114
+ return calendar.rest?.action ?? null;
115
+ }
116
+ export function travelCost(state, path) {
117
+ let total = 0;
118
+ for (let index = 1; index < path.length; index += 1) {
119
+ const from = path[index - 1];
120
+ const to = path[index];
121
+ total += state.places[from]?.edgeCosts?.[to] ?? beats(state.calendar, state.travelSteps);
122
+ }
123
+ return total;
124
+ }
@@ -0,0 +1,5 @@
1
+ import type { LivingStateRaw } from './types.js';
2
+ export declare function scriptOf(code: number): string | null;
3
+ export declare function scriptsIn(text: string): Set<string>;
4
+ export declare function worldScripts(state: LivingStateRaw): Set<string>;
5
+ export declare function foreignScripts(expected: ReadonlySet<string>, text: string): string[];
@@ -0,0 +1,115 @@
1
+ const SCRIPT_RANGES = [
2
+ { from: 0x0041, to: 0x005a, script: 'Latin' },
3
+ { from: 0x0061, to: 0x007a, script: 'Latin' },
4
+ { from: 0x00c0, to: 0x024f, script: 'Latin' },
5
+ { from: 0x1e00, to: 0x1eff, script: 'Latin' },
6
+ { from: 0x2c60, to: 0x2c7f, script: 'Latin' },
7
+ { from: 0xa720, to: 0xa7ff, script: 'Latin' },
8
+ { from: 0x0370, to: 0x03ff, script: 'Greek' },
9
+ { from: 0x1f00, to: 0x1fff, script: 'Greek' },
10
+ { from: 0x0400, to: 0x052f, script: 'Cyrillic' },
11
+ { from: 0x2de0, to: 0x2dff, script: 'Cyrillic' },
12
+ { from: 0xa640, to: 0xa69f, script: 'Cyrillic' },
13
+ { from: 0x0530, to: 0x058f, script: 'Armenian' },
14
+ { from: 0x0590, to: 0x05ff, script: 'Hebrew' },
15
+ { from: 0x0600, to: 0x06ff, script: 'Arabic' },
16
+ { from: 0x0750, to: 0x077f, script: 'Arabic' },
17
+ { from: 0x08a0, to: 0x08ff, script: 'Arabic' },
18
+ { from: 0xfb50, to: 0xfdff, script: 'Arabic' },
19
+ { from: 0xfe70, to: 0xfeff, script: 'Arabic' },
20
+ { from: 0x0700, to: 0x074f, script: 'Syriac' },
21
+ { from: 0x0780, to: 0x07bf, script: 'Thaana' },
22
+ { from: 0x07c0, to: 0x07ff, script: 'NKo' },
23
+ { from: 0x0900, to: 0x097f, script: 'Devanagari' },
24
+ { from: 0x0980, to: 0x09ff, script: 'Bengali' },
25
+ { from: 0x0a00, to: 0x0a7f, script: 'Gurmukhi' },
26
+ { from: 0x0a80, to: 0x0aff, script: 'Gujarati' },
27
+ { from: 0x0b00, to: 0x0b7f, script: 'Oriya' },
28
+ { from: 0x0b80, to: 0x0bff, script: 'Tamil' },
29
+ { from: 0x0c00, to: 0x0c7f, script: 'Telugu' },
30
+ { from: 0x0c80, to: 0x0cff, script: 'Kannada' },
31
+ { from: 0x0d00, to: 0x0d7f, script: 'Malayalam' },
32
+ { from: 0x0d80, to: 0x0dff, script: 'Sinhala' },
33
+ { from: 0x0e00, to: 0x0e7f, script: 'Thai' },
34
+ { from: 0x0e80, to: 0x0eff, script: 'Lao' },
35
+ { from: 0x0f00, to: 0x0fff, script: 'Tibetan' },
36
+ { from: 0x1000, to: 0x109f, script: 'Myanmar' },
37
+ { from: 0x10a0, to: 0x10ff, script: 'Georgian' },
38
+ { from: 0x1100, to: 0x11ff, script: 'Hangul' },
39
+ { from: 0x3130, to: 0x318f, script: 'Hangul' },
40
+ { from: 0xac00, to: 0xd7af, script: 'Hangul' },
41
+ { from: 0x1200, to: 0x137f, script: 'Ethiopic' },
42
+ { from: 0x13a0, to: 0x13ff, script: 'Cherokee' },
43
+ { from: 0x1400, to: 0x167f, script: 'CanadianAboriginal' },
44
+ { from: 0x1680, to: 0x169f, script: 'Ogham' },
45
+ { from: 0x16a0, to: 0x16ff, script: 'Runic' },
46
+ { from: 0x1780, to: 0x17ff, script: 'Khmer' },
47
+ { from: 0x1800, to: 0x18af, script: 'Mongolian' },
48
+ { from: 0x2c00, to: 0x2c5f, script: 'Glagolitic' },
49
+ { from: 0x2c80, to: 0x2cff, script: 'Coptic' },
50
+ { from: 0x2d30, to: 0x2d7f, script: 'Tifinagh' },
51
+ { from: 0x3040, to: 0x309f, script: 'Hiragana' },
52
+ { from: 0x30a0, to: 0x30ff, script: 'Katakana' },
53
+ { from: 0x31f0, to: 0x31ff, script: 'Katakana' },
54
+ { from: 0x3100, to: 0x312f, script: 'Bopomofo' },
55
+ { from: 0x3400, to: 0x4dbf, script: 'Han' },
56
+ { from: 0x4e00, to: 0x9fff, script: 'Han' },
57
+ { from: 0xf900, to: 0xfaff, script: 'Han' },
58
+ { from: 0x20000, to: 0x2fa1f, script: 'Han' },
59
+ { from: 0xa000, to: 0xa48f, script: 'Yi' },
60
+ { from: 0xa500, to: 0xa63f, script: 'Vai' },
61
+ ];
62
+ export function scriptOf(code) {
63
+ for (const range of SCRIPT_RANGES)
64
+ if (code >= range.from && code <= range.to)
65
+ return range.script;
66
+ return null;
67
+ }
68
+ export function scriptsIn(text) {
69
+ const scripts = new Set();
70
+ for (const character of text) {
71
+ const code = character.codePointAt(0);
72
+ if (code === undefined)
73
+ continue;
74
+ const script = scriptOf(code);
75
+ if (script !== null)
76
+ scripts.add(script);
77
+ }
78
+ return scripts;
79
+ }
80
+ export function worldScripts(state) {
81
+ const texts = [state.opening.title, state.opening.situation, state.opening.stakes];
82
+ for (const place of Object.values(state.places)) {
83
+ if (place.introducedAt === 0)
84
+ texts.push(place.name, place.summary, ...place.conditions);
85
+ }
86
+ for (const object of Object.values(state.objects)) {
87
+ if (object.introducedAt === 0)
88
+ texts.push(object.name, object.summary, object.condition ?? '');
89
+ }
90
+ for (const entity of Object.values(state.entities)) {
91
+ if (entity.introducedSeq <= 1)
92
+ texts.push(entity.name);
93
+ }
94
+ texts.push(...state.facts);
95
+ for (const tension of state.tensions)
96
+ texts.push(tension.title, tension.summary);
97
+ texts.push(state.people[state.playerId]?.name ?? '');
98
+ const scripts = new Set();
99
+ for (const text of texts)
100
+ for (const script of scriptsIn(text))
101
+ scripts.add(script);
102
+ return scripts;
103
+ }
104
+ export function foreignScripts(expected, text) {
105
+ const found = new Set();
106
+ for (const character of text) {
107
+ const code = character.codePointAt(0);
108
+ if (code === undefined)
109
+ continue;
110
+ const script = scriptOf(code);
111
+ if (script !== null && !expected.has(script))
112
+ found.add(script);
113
+ }
114
+ return [...found];
115
+ }
@@ -1,24 +1,17 @@
1
1
  import { type LivingKindDefinition } from './open-world.js';
2
2
  export declare const SHARED_KINDS: readonly LivingKindDefinition[];
3
- export declare function sharedKindMap(engine?: number): Record<string, LivingKindDefinition>;
4
- export declare const SLOT_RULES_ENGINE = 42;
3
+ export declare function sharedKindMap(): Record<string, LivingKindDefinition>;
5
4
  export declare function staysInPlace(kind: {
6
5
  mechanics: readonly {
7
6
  kind: string;
8
7
  }[];
9
8
  neverHeld?: boolean;
10
9
  } | undefined): boolean;
11
- export declare function neverHeld(engine: number, kindId: string, kind: {
10
+ export declare function neverHeld(kind: {
12
11
  mechanics: readonly {
13
12
  kind: string;
14
13
  }[];
15
14
  neverHeld?: boolean;
16
15
  } | undefined): boolean;
17
- export declare function sealedPaper(engine: number, entity: {
18
- kindId: string;
19
- properties: Record<string, {
20
- value: unknown;
21
- } | undefined>;
22
- }): boolean;
23
- export declare function namesPerson(engine: number, kindId: string, propertyId: string): boolean;
24
- export declare function oneEachSlot(engine: number, kindId: string): string | null;
16
+ export declare function namesPerson(kindId: string, propertyId: string): boolean;
17
+ export declare function oneEachSlot(kindId: string): string | null;
@@ -1,96 +1,26 @@
1
+ import LIBRARY_JSON from './library.json' with { type: 'json' };
1
2
  import { kindDefinitionSchema } from './open-world.js';
2
- import { BODILESS_ENGINE, LIVING_ENGINE_VERSION, VOTES_ENGINE } from './types.js';
3
- // These definitions are data. The reducer gives each mechanic the same meaning
4
- // regardless of which kind attaches it.
5
- export const SHARED_KINDS = [
6
- {
7
- id: 'fire', name: 'Fire', meaning: 'Heat and flame that may grow or move.',
8
- properties: [{ id: 'intensity', type: 'level' }, { id: 'severity', type: 'number' }], relations: [{ id: 'connected-to', targetKindId: 'fire' }],
9
- mechanics: [{ kind: 'spread', propertyId: 'intensity', relationId: 'connected-to' }, { kind: 'signal', propertyId: 'intensity', relationId: 'connected-to', channel: 'sight' },
10
- { kind: 'trigger', propertyId: 'severity', threshold: 1, direction: 'at-least', intent: 'Check the consequences of this danger.', delayMinutes: 15, minImportance: 7 }],
11
- },
12
- {
13
- id: 'water', name: 'Water', meaning: 'Liquid that may flow to connected places.',
14
- properties: [{ id: 'amount', type: 'level' }], relations: [{ id: 'connected-to', targetKindId: 'water' }],
15
- mechanics: [{ kind: 'spread', propertyId: 'amount', relationId: 'connected-to' }],
16
- },
17
- {
18
- id: 'smoke', name: 'Smoke', meaning: 'Airborne matter that can be seen or smelled.',
19
- properties: [{ id: 'density', type: 'level' }, { id: 'severity', type: 'number' }], relations: [{ id: 'connected-to', targetKindId: 'smoke' }],
20
- mechanics: [{ kind: 'spread', propertyId: 'density', relationId: 'connected-to' }, { kind: 'signal', propertyId: 'density', relationId: 'connected-to', channel: 'smell' },
21
- { kind: 'trigger', propertyId: 'severity', threshold: 1, direction: 'at-least', intent: 'Check the consequences of this danger.', delayMinutes: 15, minImportance: 7 }],
22
- },
23
- {
24
- id: 'sound', name: 'Sound', meaning: 'A disturbance that can be heard along a channel.',
25
- properties: [{ id: 'strength', type: 'level' }], relations: [{ id: 'connected-to', targetKindId: 'sound' }],
26
- mechanics: [{ kind: 'signal', propertyId: 'strength', relationId: 'connected-to', channel: 'sound' }],
27
- },
28
- { id: 'injury', name: 'Injury', meaning: 'Physical harm to a living person.', properties: [{ id: 'severity', type: 'level' }, { id: 'person', type: 'id' }], relations: [],
29
- mechanics: [{ kind: 'gate', propertyId: 'severity', subjectPropertyId: 'person', atLeast: 'high', blockedActionKinds: ['incapacitate'] }] },
30
- { id: 'illness', name: 'Illness', meaning: 'A condition that affects a living person over time.', properties: [{ id: 'severity', type: 'level' }, { id: 'person', type: 'id' }], relations: [],
31
- mechanics: [{ kind: 'gate', propertyId: 'severity', subjectPropertyId: 'person', atLeast: 'high', blockedActionKinds: ['incapacitate'] }] },
32
- { id: 'posture', name: 'Posture', meaning: 'A person’s current physical position.', properties: [{ id: 'state', type: 'level' }, { id: 'person', type: 'id' }], relations: [],
33
- mechanics: [{ kind: 'change', propertyId: 'state', deltaPerMinute: 0, onEvent: { kind: 'move', subjectPropertyId: 'person', value: 'none' } }] },
34
- { id: 'custody', name: 'Custody', meaning: 'Control of a person or thing by another.', properties: [{ id: 'holder', type: 'id' }, { id: 'held', type: 'id' }], relations: [],
35
- mechanics: [{ kind: 'gate', propertyId: 'holder', subjectPropertyId: 'held', blockedActionKinds: ['move'] }] },
36
- { id: 'money', name: 'Money', meaning: 'A transferable amount of value.', properties: [{ id: 'amount', type: 'number' }, { id: 'owner', type: 'id' }], relations: [], mechanics: [] },
37
- { id: 'debt', name: 'Debt', meaning: 'An amount one party owes another.', properties: [{ id: 'quantity', type: 'number' }, { id: 'debtor', type: 'id' }, { id: 'recipient', type: 'id' }, { id: 'creditor', type: 'id' }], relations: [], mechanics: [] },
38
- { id: 'authority', name: 'Authority', meaning: 'A grant of power to act for a group.', properties: [{ id: 'active', type: 'flag' }, { id: 'holder', type: 'id' }], relations: [], mechanics: [] },
39
- { id: 'detention', name: 'Detention', meaning: 'A person is held under another’s authority.', properties: [{ id: 'active', type: 'flag' }, { id: 'subject', type: 'id' }, { id: 'holder', type: 'id' }], relations: [],
40
- mechanics: [{ kind: 'gate', propertyId: 'active', subjectPropertyId: 'subject', blockedActionKinds: ['move'] }] },
41
- { id: 'document', name: 'Document', meaning: 'A written record with content, an author and a date.', properties: [{ id: 'body', type: 'text', requiredOnCreate: true }, { id: 'author', type: 'id' }, { id: 'recorded-at', type: 'time' }, { id: 'signed', type: 'flag' }], relations: [], mechanics: [] },
42
- { id: 'decision', name: 'Decision', meaning: 'A question its members settle by vote: each casts their own ballot, and the count decides it, not anyone\'s word.',
43
- properties: [{ id: 'question', type: 'text', requiredOnCreate: true }, { id: 'needed', type: 'number' }, { id: 'opens-at', type: 'time' }, { id: 'closes-at', type: 'time' }], relations: [], mechanics: [] },
44
- { id: 'item', name: 'Item', meaning: 'An ordinary physical thing, such as a tool, food, a container or a device; its condition says how it is now.', properties: [], relations: [], mechanics: [] },
45
- ].map(definition => kindDefinitionSchema.parse(definition));
46
- // The engine that began a world decides its shared kinds, so an older save replays with the kinds it began with.
47
- const CLOCK_TIME_ENGINE = 38;
48
- // From engine 40 a thing in hand is simply held, so custody means only a person kept by another.
49
- const PERSON_CUSTODY_ENGINE = 40;
50
- const PERSON_CUSTODY = 'A person kept from leaving by another person; a thing in hand needs no custody.';
51
- // From engine 41 an ordinary thing has a kind of its own.
52
- const ITEM_ENGINE = 41;
53
- // From engine 42 a passing sound or a posture is no lasting thing: notices and conditions carry them.
54
- const PASSING_KINDS_ENGINE = 42;
3
+ export const SHARED_KINDS = kindDefinitionSchema.array().parse(LIBRARY_JSON);
55
4
  const PASSING_KINDS = new Set(['sound', 'posture']);
56
- export function sharedKindMap(engine = LIVING_ENGINE_VERSION) {
57
- return Object.fromEntries(SHARED_KINDS.filter(kind => (kind.id !== 'item' || engine >= ITEM_ENGINE) && (kind.id !== 'decision' || engine >= VOTES_ENGINE)
58
- && (!PASSING_KINDS.has(kind.id) || engine < PASSING_KINDS_ENGINE)).map((kind) => {
59
- const timed = engine >= CLOCK_TIME_ENGINE ? kind
60
- : { ...kind, properties: kind.properties.map(property => property.type === 'time' ? { ...property, type: 'number' } : property) };
61
- const counted = kind.id === 'item' && engine >= SLOT_RULES_ENGINE ? { ...timed, properties: ITEM_SLOTS }
62
- : kind.id === 'document' && engine >= SLOT_RULES_ENGINE ? { ...timed, meaning: SEALABLE_DOCUMENT, properties: [...timed.properties, { id: 'sealed', type: 'flag' }] } : timed;
63
- return [kind.id, kind.id === 'custody' && engine >= PERSON_CUSTODY_ENGINE ? { ...counted, meaning: PERSON_CUSTODY } : counted];
64
- }));
5
+ export function sharedKindMap() {
6
+ return Object.fromEntries(SHARED_KINDS.filter(kind => !PASSING_KINDS.has(kind.id)).map((kind) => [kind.id, kind]));
65
7
  }
66
- // From engine 42 an ordinary thing can be counted and owned, an id slot of a shared kind that names a
67
- // person must name one, and a person has one injury and one illness, which later rulings change.
68
- export const SLOT_RULES_ENGINE = 42;
69
- const ITEM_SLOTS = [{ id: 'count', type: 'number' }, { id: 'owner', type: 'id' }];
70
8
  const PERSON_SLOTS = {
71
- injury: ['person'], illness: ['person'], custody: ['holder', 'held'], detention: ['subject', 'holder'], authority: ['holder'], money: ['owner'], item: ['owner'],
9
+ injury: ['person'], illness: ['person'], custody: ['holder', 'held'], detention: ['subject', 'holder'], authority: ['holder'], item: ['owner'],
72
10
  };
73
11
  const ONE_EACH = { injury: 'person', illness: 'person' };
74
- // A sealed paper holds the words it was made with; no one reads or adds to them until it is opened.
75
- const SEALABLE_DOCUMENT = 'A written record with content, an author and a date; a sealed one keeps the words it was made with until someone opens it.';
76
12
  // A kind that spreads or carries through places, such as fire, water, smoke or sound, stays where it is: no one holds it.
77
13
  export function staysInPlace(kind) {
78
14
  return kind?.neverHeld === true || (kind?.mechanics ?? []).some(mechanic => mechanic.kind === 'spread' || mechanic.kind === 'signal');
79
15
  }
80
- // Shared kinds that are a state of a person or a bond between people, not a thing: no one holds them.
81
- const BODILESS_KINDS = new Set(['injury', 'illness', 'posture', 'custody', 'detention', 'authority', 'debt', 'decision']);
82
16
  // Whether no one can hold a thing of this kind in a world begun on this engine.
83
- export function neverHeld(engine, kindId, kind) {
84
- return staysInPlace(kind) || engine >= BODILESS_ENGINE && BODILESS_KINDS.has(kindId);
17
+ export function neverHeld(kind) {
18
+ return staysInPlace(kind) || kind?.neverHeld === true;
85
19
  }
86
- // Whether a thing is a paper still sealed, whose words no one may read or add to yet.
87
- export function sealedPaper(engine, entity) {
88
- return engine >= SLOT_RULES_ENGINE && entity.kindId === 'document' && entity.properties.sealed?.value === true;
89
- }
90
- export function namesPerson(engine, kindId, propertyId) {
91
- return engine >= SLOT_RULES_ENGINE && (PERSON_SLOTS[kindId]?.includes(propertyId) ?? false);
20
+ export function namesPerson(kindId, propertyId) {
21
+ return PERSON_SLOTS[kindId]?.includes(propertyId) ?? false;
92
22
  }
93
23
  // The slot of which a person may have only one thing of this kind, if any.
94
- export function oneEachSlot(engine, kindId) {
95
- return engine >= SLOT_RULES_ENGINE ? ONE_EACH[kindId] ?? null : null;
24
+ export function oneEachSlot(kindId) {
25
+ return ONE_EACH[kindId] ?? null;
96
26
  }
@@ -0,0 +1,14 @@
1
+ import type { LivingStateRaw as State } from './types.js';
2
+ export type Located = {
3
+ containerId: string | null;
4
+ };
5
+ export declare function containerOf(state: State, id: string): string | null;
6
+ export declare function holds(state: State, containerId: string): string[];
7
+ export declare function heldBy(state: State, personId: string): string[];
8
+ export declare function entitiesHere(state: State, placeId: string): string[];
9
+ export declare function objectsHere(state: State, placeId: string): string[];
10
+ export declare function siteOf(state: State, id: string | null): string | null;
11
+ export declare function inside(state: State, containerId: string, ids: readonly string[]): string[];
12
+ export declare function moveWithin(state: State, id: string, to: string | null): void;
13
+ export declare function placesWithin(state: State, from: string, range: number): string[];
14
+ export declare function channelRange(state: State, channelId: string): number | null;
@@ -0,0 +1,132 @@
1
+ function savedContainer(thing) {
2
+ if (thing.containerId !== undefined)
3
+ return thing.containerId;
4
+ if (thing.holderId !== undefined && thing.holderId !== null)
5
+ return thing.holderId;
6
+ return thing.placeId ?? null;
7
+ }
8
+ export function containerOf(state, id) {
9
+ const person = state.people[id];
10
+ if (person)
11
+ return person.placeId;
12
+ const place = state.places[id];
13
+ if (place)
14
+ return savedContainer(place);
15
+ const object = state.objects[id];
16
+ if (object)
17
+ return savedContainer(object);
18
+ const entity = state.entities[id];
19
+ if (entity)
20
+ return savedContainer(entity);
21
+ return null;
22
+ }
23
+ export function holds(state, containerId) {
24
+ const within = state.within;
25
+ if (within !== undefined)
26
+ return within[containerId] ?? [];
27
+ return [
28
+ ...Object.values(state.people).filter(person => person.placeId === containerId).map(person => person.id),
29
+ ...Object.values(state.places).filter(place => savedContainer(place) === containerId).map(place => place.id),
30
+ ...Object.values(state.objects).filter(object => savedContainer(object) === containerId).map(object => object.id),
31
+ ...Object.values(state.entities).filter(entity => savedContainer(entity) === containerId).map(entity => entity.id),
32
+ ];
33
+ }
34
+ export function heldBy(state, personId) {
35
+ return holds(state, personId);
36
+ }
37
+ export function entitiesHere(state, placeId) {
38
+ const direct = new Set(holds(state, placeId).filter(id => state.entities[id] !== undefined));
39
+ const held = Object.values(state.people).filter(person => person.placeId === placeId)
40
+ .flatMap(person => holds(state, person.id).filter(id => state.entities[id] !== undefined));
41
+ return [...new Set([...direct, ...held])];
42
+ }
43
+ export function objectsHere(state, placeId) {
44
+ const direct = new Set([...holds(state, placeId).filter(id => state.objects[id] !== undefined),
45
+ ...Object.values(state.objects).filter(object => object.containerId === placeId).map(object => object.id)]);
46
+ const held = Object.values(state.people).filter(person => person.placeId === placeId)
47
+ .flatMap(person => holds(state, person.id).filter(id => state.objects[id] !== undefined));
48
+ return [...new Set([...direct, ...held])];
49
+ }
50
+ export function siteOf(state, id) {
51
+ let current = id;
52
+ for (let depth = 0; current !== null && depth < 64; depth += 1) {
53
+ if (state.places[current] !== undefined)
54
+ return current;
55
+ current = containerOf(state, current);
56
+ }
57
+ return null;
58
+ }
59
+ export function inside(state, containerId, ids) {
60
+ return ids.filter(id => containerOf(state, id) === containerId);
61
+ }
62
+ function detach(state, id, from) {
63
+ if (from === null)
64
+ return;
65
+ const held = state.within[from];
66
+ if (held === undefined)
67
+ return;
68
+ const left = held.filter(child => child !== id);
69
+ if (left.length > 0)
70
+ state.within[from] = left;
71
+ else
72
+ delete state.within[from];
73
+ }
74
+ function attach(state, id, to) {
75
+ if (to === null)
76
+ return;
77
+ const held = state.within[to];
78
+ if (held === undefined)
79
+ state.within[to] = [id];
80
+ else if (!held.includes(id))
81
+ state.within[to] = [...held, id];
82
+ }
83
+ export function moveWithin(state, id, to) {
84
+ const from = containerOf(state, id);
85
+ if (from === to)
86
+ return;
87
+ detach(state, id, from);
88
+ attach(state, id, to);
89
+ const person = state.people[id];
90
+ if (person) {
91
+ if (to === null)
92
+ throw new Error(`Person ${id} must be inside a place`);
93
+ person.placeId = to;
94
+ return;
95
+ }
96
+ const place = state.places[id];
97
+ if (place) {
98
+ place.containerId = to;
99
+ return;
100
+ }
101
+ const object = state.objects[id];
102
+ if (object) {
103
+ object.containerId = to;
104
+ return;
105
+ }
106
+ const entity = state.entities[id];
107
+ if (entity)
108
+ entity.containerId = to;
109
+ }
110
+ export function placesWithin(state, from, range) {
111
+ if (range <= 0)
112
+ return [from];
113
+ const seen = new Set([from]);
114
+ let frontier = [from];
115
+ for (let step = 0; step < range && frontier.length > 0; step += 1) {
116
+ const next = [];
117
+ for (const id of frontier) {
118
+ for (const connection of state.places[id]?.connections ?? []) {
119
+ if (seen.has(connection))
120
+ continue;
121
+ seen.add(connection);
122
+ next.push(connection);
123
+ }
124
+ }
125
+ frontier = next;
126
+ }
127
+ return [...seen];
128
+ }
129
+ export function channelRange(state, channelId) {
130
+ const channel = state.channels[channelId];
131
+ return channel === undefined ? null : channel.reach;
132
+ }