@spellkave/sdk 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +89 -0
- package/README.md +155 -2
- package/package.json +63 -4
- package/src/binding_rows.ts +186 -0
- package/src/connection.ts +964 -0
- package/src/core/actor.ts +62 -0
- package/src/core/actor_control.ts +193 -0
- package/src/core/actor_control_query_methods.ts +101 -0
- package/src/core/actor_control_reaction_methods.ts +79 -0
- package/src/core/actor_control_sense_methods.ts +81 -0
- package/src/core/actor_control_source.ts +57 -0
- package/src/core/actor_session_methods.ts +226 -0
- package/src/core/binding_drift.ts +301 -0
- package/src/core/credential.ts +259 -0
- package/src/core/driver_control.ts +54 -0
- package/src/core/effective_environment_state.ts +71 -0
- package/src/core/factored_manipulation_attempt.ts +158 -0
- package/src/core/ids.ts +60 -0
- package/src/core/interaction/canonical.ts +15 -0
- package/src/core/interaction/fingerprint.ts +118 -0
- package/src/core/interaction/index.ts +31 -0
- package/src/core/interaction/intent.ts +284 -0
- package/src/core/interaction/outcome.ts +67 -0
- package/src/core/interaction/rejection_ledger.ts +108 -0
- package/src/core/kernel_contract.generated.ts +10 -0
- package/src/core/log.ts +23 -0
- package/src/core/movement.ts +111 -0
- package/src/core/node-io.ts +208 -0
- package/src/core/paths.ts +220 -0
- package/src/core/profile.ts +270 -0
- package/src/core/profiles.ts +9 -0
- package/src/core/provenance_buffer.ts +102 -0
- package/src/core/readers/_helpers.ts +273 -0
- package/src/core/readers/action_description.ts +17 -0
- package/src/core/readers/action_economy_events.ts +149 -0
- package/src/core/readers/actor_body_swaps.ts +62 -0
- package/src/core/readers/actor_snapshot.ts +197 -0
- package/src/core/readers/actor_states.ts +163 -0
- package/src/core/readers/actors_and_bodies.ts +179 -0
- package/src/core/readers/affordance_cue.ts +59 -0
- package/src/core/readers/affordance_defaults.ts +158 -0
- package/src/core/readers/affordance_group_membership.ts +67 -0
- package/src/core/readers/affordance_input.ts +129 -0
- package/src/core/readers/affordance_reference.ts +41 -0
- package/src/core/readers/apply_consequences.ts +54 -0
- package/src/core/readers/audience_trace_presentation.ts +62 -0
- package/src/core/readers/bounded_collection.ts +29 -0
- package/src/core/readers/bundle_definitions.ts +74 -0
- package/src/core/readers/bundle_entities.ts +62 -0
- package/src/core/readers/co_location.ts +71 -0
- package/src/core/readers/combat_session.ts +124 -0
- package/src/core/readers/combat_session_active.ts +44 -0
- package/src/core/readers/combat_session_feedback.ts +37 -0
- package/src/core/readers/combat_session_grid.ts +117 -0
- package/src/core/readers/combat_session_roster.ts +98 -0
- package/src/core/readers/combat_session_types.ts +51 -0
- package/src/core/readers/condition_deltas.ts +53 -0
- package/src/core/readers/container_contents.ts +92 -0
- package/src/core/readers/dialogue_guide.ts +99 -0
- package/src/core/readers/dialogue_prompt_presentation.ts +90 -0
- package/src/core/readers/driver_control.ts +64 -0
- package/src/core/readers/entity_art.ts +219 -0
- package/src/core/readers/entity_containment.ts +119 -0
- package/src/core/readers/equipment.ts +99 -0
- package/src/core/readers/intervention_windows.ts +136 -0
- package/src/core/readers/inventory_query.ts +145 -0
- package/src/core/readers/item_detail.ts +198 -0
- package/src/core/readers/location_snapshot.ts +216 -0
- package/src/core/readers/manifest_apply_provenance.ts +32 -0
- package/src/core/readers/manifest_export.ts +81 -0
- package/src/core/readers/manipulation_context.ts +475 -0
- package/src/core/readers/map_layers.ts +49 -0
- package/src/core/readers/metadata_rows.ts +56 -0
- package/src/core/readers/object_state.ts +124 -0
- package/src/core/readers/observer_knowledge.ts +41 -0
- package/src/core/readers/observer_place_knowledge.ts +37 -0
- package/src/core/readers/observer_trace_knowledge.ts +45 -0
- package/src/core/readers/outcome_template.ts +41 -0
- package/src/core/readers/place_topology.ts +109 -0
- package/src/core/readers/reaction_options.ts +156 -0
- package/src/core/readers/resource_deltas.ts +47 -0
- package/src/core/readers/scene.ts +275 -0
- package/src/core/readers/seats.ts +33 -0
- package/src/core/readers/self.ts +245 -0
- package/src/core/readers/self_capabilities.ts +159 -0
- package/src/core/readers/self_economy.ts +101 -0
- package/src/core/readers/self_economy_budget.ts +93 -0
- package/src/core/readers/self_evaluations.ts +128 -0
- package/src/core/readers/self_identity.ts +104 -0
- package/src/core/readers/sense_context.ts +112 -0
- package/src/core/readers/session.ts +207 -0
- package/src/core/readers/session_detail.ts +211 -0
- package/src/core/readers/session_status.ts +68 -0
- package/src/core/readers/spatial_environment_state.ts +74 -0
- package/src/core/readers/spatial_niche_attachments.ts +28 -0
- package/src/core/readers/substrate_definitions.ts +27 -0
- package/src/core/readers/suggest_next_action.ts +72 -0
- package/src/core/readers/surface_membership.ts +78 -0
- package/src/core/readers/tag_reads.ts +54 -0
- package/src/core/readers/trace_query.ts +120 -0
- package/src/core/readers/trace_response.ts +80 -0
- package/src/core/readers/traces.ts +124 -0
- package/src/core/readers/travel_session.ts +88 -0
- package/src/core/readers/turn_state.ts +34 -0
- package/src/core/readers/world_events.ts +43 -0
- package/src/core/readers/world_status.ts +63 -0
- package/src/core/reducers/manipulation_args.ts +239 -0
- package/src/core/reducers/manipulation_receipt.ts +66 -0
- package/src/core/reducers/r_actor_wake_up.ts +32 -0
- package/src/core/reducers/r_inscribe_trace.ts +109 -0
- package/src/core/reducers/r_manipulate.ts +163 -0
- package/src/core/reducers/r_move.ts +155 -0
- package/src/core/reducers/r_react.ts +143 -0
- package/src/core/reducers/r_release_claim.ts +81 -0
- package/src/core/reducers/r_sense.ts +84 -0
- package/src/core/reducers/r_session_close.ts +130 -0
- package/src/core/reducers/r_session_move.ts +116 -0
- package/src/core/reducers/r_session_open.ts +91 -0
- package/src/core/reducers/session_lookup.ts +64 -0
- package/src/core/reducers/tick.ts +65 -0
- package/src/core/reducers/trace_lookup.ts +29 -0
- package/src/core/reducers/transport.ts +84 -0
- package/src/core/resolve.ts +228 -0
- package/src/core/shape_telemetry.ts +146 -0
- package/src/core/snapshot_rows.ts +109 -0
- package/src/core/tick_cadence.ts +61 -0
- package/src/core/types.ts +501 -0
- package/src/core/watch_provenance.ts +113 -0
- package/src/core/world-contract/classify.ts +38 -0
- package/src/core/world-contract/index.ts +12 -0
- package/src/core/world-contract/normalise.ts +574 -0
- package/src/core/world-contract/runtime.ts +144 -0
- package/src/core/world-contract/served.ts +173 -0
- package/src/core/world-contract/strict-reducers.ts +14 -0
- package/src/core/world-contract/typetext.ts +94 -0
- package/src/core/world_time.ts +22 -0
- package/src/index.ts +262 -0
- package/src/manifest-apply.ts +497 -0
- package/src/provenance.ts +568 -0
- package/src/reader-manifest.ts +338 -0
- package/src/readers.ts +392 -0
- package/src/replay.ts +515 -0
- package/src/watch-buffer.ts +438 -0
- package/src/world-events/builder.ts +1042 -0
|
@@ -0,0 +1,1042 @@
|
|
|
1
|
+
// Keep one builder per session. Its first call establishes a snapshot baseline.
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
isCausedByActor as runtimeIsCausedByActor,
|
|
5
|
+
type WorldEvent,
|
|
6
|
+
type WorldEventBuilder,
|
|
7
|
+
} from "@spellkave/world-events";
|
|
8
|
+
import { asEntityRef, type EntityRef } from "../core/ids";
|
|
9
|
+
import {
|
|
10
|
+
buildEntityNameMap,
|
|
11
|
+
readActiveSessionWithRosterForParticipant,
|
|
12
|
+
readCurrentScene,
|
|
13
|
+
readInterventionWindowsForActor,
|
|
14
|
+
readManipulationContext,
|
|
15
|
+
readReactionOptionsForActor,
|
|
16
|
+
readRecentConditionDeltas,
|
|
17
|
+
readRecentResourceDeltas,
|
|
18
|
+
readRecentTraces,
|
|
19
|
+
readTurnState,
|
|
20
|
+
readWorldStatus,
|
|
21
|
+
resolveEntityId,
|
|
22
|
+
type SnapshotDb,
|
|
23
|
+
type TraceRow,
|
|
24
|
+
} from "../readers";
|
|
25
|
+
|
|
26
|
+
export type {
|
|
27
|
+
AdvancementAvailableEvent,
|
|
28
|
+
DealtConditionEvent,
|
|
29
|
+
DealtHitEvent,
|
|
30
|
+
ReactionWindowEvent,
|
|
31
|
+
SelfConditionChangedEvent,
|
|
32
|
+
SelfHitEvent,
|
|
33
|
+
SelfResourceSpentEvent,
|
|
34
|
+
SessionLifecycleEvent,
|
|
35
|
+
SpeechHeardEvent,
|
|
36
|
+
TurnTransitionEvent,
|
|
37
|
+
WorldEvent,
|
|
38
|
+
WorldEventBuilder,
|
|
39
|
+
XpGainedEvent,
|
|
40
|
+
} from "@spellkave/world-events";
|
|
41
|
+
|
|
42
|
+
// The portable WorldEvent vocabulary is authored in @spellkave/tool-runtime.
|
|
43
|
+
// This SDK-owning module only produces those events from subscribed deltas.
|
|
44
|
+
|
|
45
|
+
/** Last `/`-separated stem of a resource id (`srd/hp` → `hp`). Classification
|
|
46
|
+
* is structural — never a `"srd/…"` literal (module header). */
|
|
47
|
+
function resourceStem(resourceId: string): string {
|
|
48
|
+
const slash = resourceId.lastIndexOf("/");
|
|
49
|
+
return slash < 0 ? resourceId : resourceId.slice(slash + 1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Hit-point-shaped pools: the only resources that may ride `self_hit` /
|
|
53
|
+
* `dealt_hit`. `temp_hp` is the same durability idiom as `hp`; `hp_per_level`
|
|
54
|
+
* is a catalog fact, not a wound. */
|
|
55
|
+
function isHitPointResource(resourceId: string): boolean {
|
|
56
|
+
const stem = resourceStem(resourceId);
|
|
57
|
+
return stem === "hp" || stem === "temp_hp";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The actor's own experience pool (`srd/xp`). Distinct from a corpse's
|
|
61
|
+
* `xp_value` latch, which is another body's sheet and never pushed. */
|
|
62
|
+
function isOwnXpResource(resourceId: string): boolean {
|
|
63
|
+
return resourceStem(resourceId) === "xp";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Reader-bounded read window for the resource-delta event log per call —
|
|
67
|
+
* large enough to catch a burst (e.g. several hits in one tick) without
|
|
68
|
+
* scanning the whole unbounded log every cycle. */
|
|
69
|
+
const RESOURCE_DELTA_SCAN_WINDOW = 200;
|
|
70
|
+
|
|
71
|
+
/** Same shape as `RESOURCE_DELTA_SCAN_WINDOW` over `readRecentConditionDeltas`
|
|
72
|
+
* (K-B Part B). */
|
|
73
|
+
const CONDITION_DELTA_SCAN_WINDOW = 200;
|
|
74
|
+
|
|
75
|
+
/** Same shape as `RESOURCE_DELTA_SCAN_WINDOW` over `readRecentTraces`
|
|
76
|
+
* (module header: `visible_trace` is already per-caller bounded by
|
|
77
|
+
* perception, ADR-0040 — this is a defensive cap on a burst of several
|
|
78
|
+
* traces landing in one tick, not a correctness-load-bearing filter). */
|
|
79
|
+
const TRACE_SCAN_WINDOW = 200;
|
|
80
|
+
|
|
81
|
+
export interface WorldEventBuilderOptions {
|
|
82
|
+
/**
|
|
83
|
+
* Manipulate `action_id`s to watch for an appearance in the
|
|
84
|
+
* actor's own factored manipulation context (module header: no default vocabulary —
|
|
85
|
+
* empty/omitted means this kind never fires, matching every builder built
|
|
86
|
+
* before this option existed). A watched id fires `advancement_available`
|
|
87
|
+
* the moment it transitions from absent to present, and may fire again
|
|
88
|
+
* later if it disappears (the XP gate closing again, e.g. after the
|
|
89
|
+
* action is used) and later reappears (a further threshold crossing) —
|
|
90
|
+
* each transition is a discrete occurrence, same as every other kind this
|
|
91
|
+
* builder emits.
|
|
92
|
+
*/
|
|
93
|
+
watchedAdvancementActionIds?: readonly string[];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
interface ReactionWindowSnapshotEntry {
|
|
97
|
+
windowKind: "reaction" | "intervention";
|
|
98
|
+
originatingActionId: string;
|
|
99
|
+
originatingActor: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Call-time diff builder over a companion tool context's live subscription
|
|
104
|
+
* cache — the D-2 event-builder sibling of `createDeltaTracker`
|
|
105
|
+
* (`delta-tracker.ts`). Same lazy `db`/`actorId` accessor shape, same
|
|
106
|
+
* primed-exactly-once contract (D-5, extended here to D-2): a builder
|
|
107
|
+
* recreated mid-session silently reports nothing new on its next call. `db`/
|
|
108
|
+
* `actorId` are read lazily so a builder can be constructed before a host's
|
|
109
|
+
* session connects, matching `createDeltaTracker`'s own rationale.
|
|
110
|
+
*/
|
|
111
|
+
export function createWorldEventBuilder(
|
|
112
|
+
db: () => SnapshotDb,
|
|
113
|
+
actorId: () => string | undefined,
|
|
114
|
+
options: WorldEventBuilderOptions = {},
|
|
115
|
+
): WorldEventBuilder {
|
|
116
|
+
const watchedAdvancementActionIds = options.watchedAdvancementActionIds ?? [];
|
|
117
|
+
let primed = false;
|
|
118
|
+
let openSessionId: string | null = null;
|
|
119
|
+
let openSessionOpenedBy: string | null = null;
|
|
120
|
+
let lastTurnKey: string | null = null; // `${sessionId}:${currentActor}`
|
|
121
|
+
let lastTurnActor: string | null = null;
|
|
122
|
+
let lastReactionWindows = new Map<string, ReactionWindowSnapshotEntry>();
|
|
123
|
+
let seenResourceDeltaEventIds = new Set<string>();
|
|
124
|
+
/** K-B Part B: same event_id dedupe discipline as
|
|
125
|
+
* `seenResourceDeltaEventIds`, shared across both `self_condition_changed`
|
|
126
|
+
* and `dealt_condition`. */
|
|
127
|
+
let seenConditionDeltaEventIds = new Set<string>();
|
|
128
|
+
let lastAdvancementActions = new Map<string, string>();
|
|
129
|
+
let seenTextTraceIds = new Set<string>();
|
|
130
|
+
let seenAddressedTraceIds = new Set<string>();
|
|
131
|
+
let seenDepartureTraceIds = new Set<string>();
|
|
132
|
+
|
|
133
|
+
function currentTick(currentDb: SnapshotDb): number {
|
|
134
|
+
return readWorldStatus(currentDb)?.tick ?? 0;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function reactionSnapshot(
|
|
138
|
+
currentDb: SnapshotDb,
|
|
139
|
+
currentActorId: EntityRef,
|
|
140
|
+
): Map<string, ReactionWindowSnapshotEntry> {
|
|
141
|
+
const map = new Map<string, ReactionWindowSnapshotEntry>();
|
|
142
|
+
for (const row of readReactionOptionsForActor(currentDb, currentActorId)) {
|
|
143
|
+
map.set(String(row.pending_id), {
|
|
144
|
+
windowKind: "reaction",
|
|
145
|
+
originatingActionId: row.originating_action_id,
|
|
146
|
+
originatingActor: row.originating_actor,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
for (const row of readInterventionWindowsForActor(currentDb, currentActorId)) {
|
|
150
|
+
map.set(String(row.pending_id), {
|
|
151
|
+
windowKind: "intervention",
|
|
152
|
+
originatingActionId: row.originating_action_id,
|
|
153
|
+
originatingActor: row.originating_actor,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return map;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function ownNegativeResourceDeltaEventIds(
|
|
160
|
+
currentDb: SnapshotDb,
|
|
161
|
+
currentActorId: EntityRef,
|
|
162
|
+
): Map<
|
|
163
|
+
string,
|
|
164
|
+
{
|
|
165
|
+
resourceId: string;
|
|
166
|
+
amount: number;
|
|
167
|
+
newValue: number | undefined;
|
|
168
|
+
templateId: string;
|
|
169
|
+
attackerId: string | undefined;
|
|
170
|
+
}
|
|
171
|
+
> {
|
|
172
|
+
const entityId = resolveEntityId(currentDb, currentActorId);
|
|
173
|
+
const out = new Map<
|
|
174
|
+
string,
|
|
175
|
+
{
|
|
176
|
+
resourceId: string;
|
|
177
|
+
amount: number;
|
|
178
|
+
newValue: number | undefined;
|
|
179
|
+
templateId: string;
|
|
180
|
+
attackerId: string | undefined;
|
|
181
|
+
}
|
|
182
|
+
>();
|
|
183
|
+
if (entityId === undefined) return out;
|
|
184
|
+
const entityIdText = String(entityId);
|
|
185
|
+
for (const row of readRecentResourceDeltas(currentDb, RESOURCE_DELTA_SCAN_WINDOW)) {
|
|
186
|
+
if (row.entity_id !== entityIdText || row.delta >= 0) continue;
|
|
187
|
+
out.set(row.event_id, {
|
|
188
|
+
resourceId: row.resource_id,
|
|
189
|
+
amount: -row.delta,
|
|
190
|
+
newValue: row.new_value,
|
|
191
|
+
templateId: row.template_id,
|
|
192
|
+
attackerId: row.source_entity_ref,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
return out;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Own-pool XP awards (claim-victory). Positive-only — a spend of XP is
|
|
199
|
+
* not a gain, and a foreign `xp_value` latch never matches this scan. */
|
|
200
|
+
function ownXpGainEventIds(
|
|
201
|
+
currentDb: SnapshotDb,
|
|
202
|
+
currentActorId: EntityRef,
|
|
203
|
+
): Map<string, { amount: number; newValue: number | undefined }> {
|
|
204
|
+
const entityId = resolveEntityId(currentDb, currentActorId);
|
|
205
|
+
const out = new Map<string, { amount: number; newValue: number | undefined }>();
|
|
206
|
+
if (entityId === undefined) return out;
|
|
207
|
+
const entityIdText = String(entityId);
|
|
208
|
+
for (const row of readRecentResourceDeltas(currentDb, RESOURCE_DELTA_SCAN_WINDOW)) {
|
|
209
|
+
if (row.entity_id !== entityIdText || row.delta <= 0) continue;
|
|
210
|
+
if (!isOwnXpResource(row.resource_id)) continue;
|
|
211
|
+
out.set(row.event_id, { amount: row.delta, newValue: row.new_value });
|
|
212
|
+
}
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Negative deltas this actor CAUSED on another body's hit-point pool
|
|
217
|
+
* (sk-dms.29). Non-HP resources on another body — a corpse's XP-worth
|
|
218
|
+
* latch — are that body's sheet and stay off this channel (sk-0m4.15). */
|
|
219
|
+
function dealtNegativeResourceDeltaEventIds(
|
|
220
|
+
currentDb: SnapshotDb,
|
|
221
|
+
currentActorId: EntityRef,
|
|
222
|
+
): Map<string, { targetName: string; amount: number; attackerId: string | undefined }> {
|
|
223
|
+
const entityId = resolveEntityId(currentDb, currentActorId);
|
|
224
|
+
const out = new Map<
|
|
225
|
+
string,
|
|
226
|
+
{ targetName: string; amount: number; attackerId: string | undefined }
|
|
227
|
+
>();
|
|
228
|
+
if (entityId === undefined) return out;
|
|
229
|
+
const entityIdText = String(entityId);
|
|
230
|
+
const names = buildEntityNameMap(currentDb);
|
|
231
|
+
for (const row of readRecentResourceDeltas(currentDb, RESOURCE_DELTA_SCAN_WINDOW)) {
|
|
232
|
+
if (row.source_entity_ref !== entityIdText) continue;
|
|
233
|
+
if (row.entity_id === entityIdText || row.delta >= 0) continue;
|
|
234
|
+
if (!isHitPointResource(row.resource_id)) continue;
|
|
235
|
+
out.set(row.event_id, {
|
|
236
|
+
targetName: names.get(row.entity_id) || "them",
|
|
237
|
+
amount: -row.delta,
|
|
238
|
+
// Always `entityIdText` given the filter above (module header's
|
|
239
|
+
// DealtHitEvent doc comment) — carried through the row rather than
|
|
240
|
+
// hardcoded, matching self_hit's own discipline.
|
|
241
|
+
attackerId: row.source_entity_ref,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** `event_condition_delta` rows on the connected actor's own body —
|
|
248
|
+
* K-B Part B, the {@link ownNegativeResourceDeltaEventIds} sibling for
|
|
249
|
+
* conditions. Both applies and removals ride the same
|
|
250
|
+
* `SelfConditionChangedEvent` kind (`applied` is the discriminator), so
|
|
251
|
+
* this scan (unlike the resource-delta pair above) makes no sign check. */
|
|
252
|
+
function ownConditionDeltaEventIds(
|
|
253
|
+
currentDb: SnapshotDb,
|
|
254
|
+
currentActorId: EntityRef,
|
|
255
|
+
): Map<string, { conditionId: string; applied: boolean; sourceEntityId: string | undefined }> {
|
|
256
|
+
const entityId = resolveEntityId(currentDb, currentActorId);
|
|
257
|
+
const out = new Map<
|
|
258
|
+
string,
|
|
259
|
+
{ conditionId: string; applied: boolean; sourceEntityId: string | undefined }
|
|
260
|
+
>();
|
|
261
|
+
if (entityId === undefined) return out;
|
|
262
|
+
const entityIdText = String(entityId);
|
|
263
|
+
for (const row of readRecentConditionDeltas(currentDb, CONDITION_DELTA_SCAN_WINDOW)) {
|
|
264
|
+
if (row.entity_id !== entityIdText) continue;
|
|
265
|
+
out.set(row.event_id, {
|
|
266
|
+
conditionId: row.condition_definition_id,
|
|
267
|
+
applied: row.applied,
|
|
268
|
+
sourceEntityId: row.source_entity_ref,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Condition changes this actor CAUSED on another body — the
|
|
275
|
+
* {@link dealtNegativeResourceDeltaEventIds} sibling for conditions. */
|
|
276
|
+
function dealtConditionDeltaEventIds(
|
|
277
|
+
currentDb: SnapshotDb,
|
|
278
|
+
currentActorId: EntityRef,
|
|
279
|
+
): Map<
|
|
280
|
+
string,
|
|
281
|
+
{
|
|
282
|
+
targetName: string;
|
|
283
|
+
conditionId: string;
|
|
284
|
+
applied: boolean;
|
|
285
|
+
sourceEntityId: string | undefined;
|
|
286
|
+
}
|
|
287
|
+
> {
|
|
288
|
+
const entityId = resolveEntityId(currentDb, currentActorId);
|
|
289
|
+
const out = new Map<
|
|
290
|
+
string,
|
|
291
|
+
{
|
|
292
|
+
targetName: string;
|
|
293
|
+
conditionId: string;
|
|
294
|
+
applied: boolean;
|
|
295
|
+
sourceEntityId: string | undefined;
|
|
296
|
+
}
|
|
297
|
+
>();
|
|
298
|
+
if (entityId === undefined) return out;
|
|
299
|
+
const entityIdText = String(entityId);
|
|
300
|
+
const names = buildEntityNameMap(currentDb);
|
|
301
|
+
for (const row of readRecentConditionDeltas(currentDb, CONDITION_DELTA_SCAN_WINDOW)) {
|
|
302
|
+
if (row.source_entity_ref !== entityIdText) continue;
|
|
303
|
+
if (row.entity_id === entityIdText) continue;
|
|
304
|
+
out.set(row.event_id, {
|
|
305
|
+
targetName: names.get(row.entity_id) || "them",
|
|
306
|
+
conditionId: row.condition_definition_id,
|
|
307
|
+
applied: row.applied,
|
|
308
|
+
// Always `entityIdText` given the filter above (DealtConditionEvent's
|
|
309
|
+
// own doc comment) — carried through the row rather than hardcoded,
|
|
310
|
+
// matching dealt_hit's own discipline.
|
|
311
|
+
sourceEntityId: row.source_entity_ref,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
return out;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** Disclosed action descriptors among `watchedAdvancementActionIds` (module
|
|
318
|
+
* header: caller-named allow-list, empty by default), keyed by `action_id`.
|
|
319
|
+
* Target identity is deliberately irrelevant: authored advancement may be
|
|
320
|
+
* self-targeted or offered through a co-located trainer. Empty when the
|
|
321
|
+
* watch-list is empty, so this is a no-op scan for every builder built
|
|
322
|
+
* before this option existed. */
|
|
323
|
+
function watchedAdvancementSnapshot(
|
|
324
|
+
currentDb: SnapshotDb,
|
|
325
|
+
currentActorId: EntityRef,
|
|
326
|
+
): Map<string, string> {
|
|
327
|
+
const map = new Map<string, string>();
|
|
328
|
+
if (watchedAdvancementActionIds.length === 0) return map;
|
|
329
|
+
const context = readManipulationContext(currentDb, currentActorId);
|
|
330
|
+
if (context.status !== "ready") return map;
|
|
331
|
+
for (const action of context.actions) {
|
|
332
|
+
if (!watchedAdvancementActionIds.includes(action.action_id)) continue;
|
|
333
|
+
map.set(action.action_id, action.display_name);
|
|
334
|
+
}
|
|
335
|
+
return map;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** `readActiveSessionWithRosterForParticipant`/`readSessionParticipants`
|
|
339
|
+
* (unlike `readReactionOptionsForActor`/`readInterventionWindowsForActor`,
|
|
340
|
+
* which self-resolve) expect an already-RESOLVED numeric entity id string,
|
|
341
|
+
* not a manifest ref (`combat_session.ts`'s own `heroId =
|
|
342
|
+
* String(resolveEntityId(db, actorRef))` precedent) — resolved once per
|
|
343
|
+
* call and reused for every session-scoped domain below. */
|
|
344
|
+
function resolvedEntityIdText(
|
|
345
|
+
currentDb: SnapshotDb,
|
|
346
|
+
currentActorId: EntityRef,
|
|
347
|
+
): string | undefined {
|
|
348
|
+
const resolved = resolveEntityId(currentDb, currentActorId);
|
|
349
|
+
return resolved === undefined ? undefined : String(resolved);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Traces this actor currently perceives (module header: `readRecentTraces`'s
|
|
353
|
+
* own per-caller `visible_trace` bound, ADR-0040) whose payload carries a
|
|
354
|
+
* `text` key — keyed by `trace_id`, the same discriminator every other
|
|
355
|
+
* domain's snapshot uses. Content-agnostic by construction: no `row.kind`/
|
|
356
|
+
* `row.bundle_id` check, matching the module header's `carry_dispatch_text`
|
|
357
|
+
* framing. */
|
|
358
|
+
function textCarryingTraceSnapshot(currentDb: SnapshotDb): Map<string, TraceRow> {
|
|
359
|
+
const map = new Map<string, TraceRow>();
|
|
360
|
+
for (const row of readRecentTraces(currentDb, TRACE_SCAN_WINDOW)) {
|
|
361
|
+
if (!hasTextPayload(row.payload)) continue;
|
|
362
|
+
map.set(row.trace_id, row);
|
|
363
|
+
}
|
|
364
|
+
return map;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** The `speech_heard` snapshot's exact mirror for NON-text addressed rows:
|
|
368
|
+
* a payload object with a finite numeric `addressee` and no string `text`.
|
|
369
|
+
* The two snapshots partition the perceivable rows this builder reports —
|
|
370
|
+
* a row carrying both keys belongs to `textCarryingTraceSnapshot` only. */
|
|
371
|
+
function addressedTraceSnapshot(currentDb: SnapshotDb): Map<string, TraceRow> {
|
|
372
|
+
const map = new Map<string, TraceRow>();
|
|
373
|
+
for (const row of readRecentTraces(currentDb, TRACE_SCAN_WINDOW)) {
|
|
374
|
+
if (hasTextPayload(row.payload)) continue;
|
|
375
|
+
if (!hasAddresseePayload(row.payload)) continue;
|
|
376
|
+
map.set(row.trace_id, row);
|
|
377
|
+
}
|
|
378
|
+
return map;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** The third and last snapshot of the trace family: a payload with neither
|
|
382
|
+
* `text` nor `addressee`, carrying a numeric `mover` and a string
|
|
383
|
+
* `via_place_edge_id` — a `core/departure` row. Ordered AFTER the other
|
|
384
|
+
* two on purpose (module header): the three predicates are applied in one
|
|
385
|
+
* fixed sequence so a row can land in at most one snapshot. */
|
|
386
|
+
function departureTraceSnapshot(currentDb: SnapshotDb): Map<string, TraceRow> {
|
|
387
|
+
const map = new Map<string, TraceRow>();
|
|
388
|
+
for (const row of readRecentTraces(currentDb, TRACE_SCAN_WINDOW)) {
|
|
389
|
+
if (hasTextPayload(row.payload)) continue;
|
|
390
|
+
if (hasAddresseePayload(row.payload)) continue;
|
|
391
|
+
if (!hasDeparturePayload(row.payload)) continue;
|
|
392
|
+
map.set(row.trace_id, row);
|
|
393
|
+
}
|
|
394
|
+
return map;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function prime(): void {
|
|
398
|
+
const currentDb = db();
|
|
399
|
+
const rawActorId = actorId();
|
|
400
|
+
if (rawActorId === undefined) {
|
|
401
|
+
primed = true;
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
const currentActorId = asEntityRef(rawActorId);
|
|
405
|
+
const entityIdText = resolvedEntityIdText(currentDb, currentActorId);
|
|
406
|
+
const active =
|
|
407
|
+
entityIdText === undefined
|
|
408
|
+
? null
|
|
409
|
+
: readActiveSessionWithRosterForParticipant(currentDb, entityIdText);
|
|
410
|
+
openSessionId = active === null ? null : String(active.session.sessionId);
|
|
411
|
+
openSessionOpenedBy = active === null ? null : active.session.openedBy;
|
|
412
|
+
const turnState = readTurnState(currentDb);
|
|
413
|
+
if (
|
|
414
|
+
turnState !== null &&
|
|
415
|
+
openSessionId !== null &&
|
|
416
|
+
turnState.scope_session_id === openSessionId
|
|
417
|
+
) {
|
|
418
|
+
lastTurnKey = `${openSessionId}:${turnState.current_actor}`;
|
|
419
|
+
lastTurnActor = turnState.current_actor;
|
|
420
|
+
} else {
|
|
421
|
+
lastTurnKey = null;
|
|
422
|
+
lastTurnActor = null;
|
|
423
|
+
}
|
|
424
|
+
lastReactionWindows = reactionSnapshot(currentDb, currentActorId);
|
|
425
|
+
seenResourceDeltaEventIds = new Set([
|
|
426
|
+
...ownNegativeResourceDeltaEventIds(currentDb, currentActorId).keys(),
|
|
427
|
+
...ownXpGainEventIds(currentDb, currentActorId).keys(),
|
|
428
|
+
]);
|
|
429
|
+
seenConditionDeltaEventIds = new Set(
|
|
430
|
+
ownConditionDeltaEventIds(currentDb, currentActorId).keys(),
|
|
431
|
+
);
|
|
432
|
+
lastAdvancementActions = watchedAdvancementSnapshot(currentDb, currentActorId);
|
|
433
|
+
seenTextTraceIds = new Set(textCarryingTraceSnapshot(currentDb).keys());
|
|
434
|
+
seenAddressedTraceIds = new Set(addressedTraceSnapshot(currentDb).keys());
|
|
435
|
+
seenDepartureTraceIds = new Set(departureTraceSnapshot(currentDb).keys());
|
|
436
|
+
primed = true;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// sk-0m4.73: events a prior `takeWhere` call left unmatched. Prepended,
|
|
440
|
+
// oldest-first, ahead of whatever the next `diffAndAdvance`/`takeWhere`
|
|
441
|
+
// call newly observes — the single buffer that makes "delivered exactly
|
|
442
|
+
// once" true across both entry points (see `WorldEventBuilder`'s doc
|
|
443
|
+
// comment).
|
|
444
|
+
let carry: WorldEvent[] = [];
|
|
445
|
+
|
|
446
|
+
/** The unbuffered diff-and-advance step both `diffAndAdvance` and
|
|
447
|
+
* `takeWhere` share. The carry handling lives in the two public entry
|
|
448
|
+
* points. Push order of one drain: `session_lifecycle` opened ->
|
|
449
|
+
* `turn_transition` -> `reaction_window` -> the resource and condition
|
|
450
|
+
* delta kinds, sorted together oldest-first by event id ->
|
|
451
|
+
* `advancement_available` -> the trace kinds (reader order) ->
|
|
452
|
+
* `session_lifecycle` closed, always last (module header). */
|
|
453
|
+
function computeNewEvents(): WorldEvent[] {
|
|
454
|
+
if (!primed) {
|
|
455
|
+
prime();
|
|
456
|
+
return [];
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const rawActorId = actorId();
|
|
460
|
+
if (rawActorId === undefined) return [];
|
|
461
|
+
const currentActorId = asEntityRef(rawActorId);
|
|
462
|
+
const currentDb = db();
|
|
463
|
+
const tick = currentTick(currentDb);
|
|
464
|
+
const events: WorldEvent[] = [];
|
|
465
|
+
// A session that closed in this drain. The state is forgotten at once
|
|
466
|
+
// (below), but the event itself is pushed LAST: whatever ended the
|
|
467
|
+
// session — the killing blow, the experience award — is told first.
|
|
468
|
+
let deferredClosed: WorldEvent | undefined;
|
|
469
|
+
|
|
470
|
+
// --- session_lifecycle ---
|
|
471
|
+
const entityIdText = resolvedEntityIdText(currentDb, currentActorId);
|
|
472
|
+
const active =
|
|
473
|
+
entityIdText === undefined
|
|
474
|
+
? null
|
|
475
|
+
: readActiveSessionWithRosterForParticipant(currentDb, entityIdText);
|
|
476
|
+
const activeSessionId = active === null ? null : String(active.session.sessionId);
|
|
477
|
+
if (activeSessionId !== null && activeSessionId !== openSessionId) {
|
|
478
|
+
events.push({
|
|
479
|
+
kind: "session_lifecycle",
|
|
480
|
+
session_id: activeSessionId,
|
|
481
|
+
phase: "opened",
|
|
482
|
+
participants: active === null ? [] : active.participants.map((p) => p.actorId),
|
|
483
|
+
opened_by: active === null ? "" : active.session.openedBy,
|
|
484
|
+
observed_at_tick: tick,
|
|
485
|
+
});
|
|
486
|
+
openSessionId = activeSessionId;
|
|
487
|
+
openSessionOpenedBy = active === null ? null : active.session.openedBy;
|
|
488
|
+
} else if (activeSessionId === null && openSessionId !== null) {
|
|
489
|
+
deferredClosed = {
|
|
490
|
+
kind: "session_lifecycle",
|
|
491
|
+
session_id: openSessionId,
|
|
492
|
+
phase: "closed",
|
|
493
|
+
participants: [],
|
|
494
|
+
opened_by: openSessionOpenedBy ?? "",
|
|
495
|
+
observed_at_tick: tick,
|
|
496
|
+
};
|
|
497
|
+
openSessionId = null;
|
|
498
|
+
openSessionOpenedBy = null;
|
|
499
|
+
lastTurnKey = null;
|
|
500
|
+
lastTurnActor = null;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// --- turn_transition (turn_state singleton, D-6). `openSessionId` is
|
|
504
|
+
// already scoped to a session this actor is a roster participant of
|
|
505
|
+
// (established just above via `readActiveSessionWithRosterForParticipant`),
|
|
506
|
+
// so matching `turn_state.scope_session_id` against it is sufficient —
|
|
507
|
+
// no second participant-membership read needed. ---
|
|
508
|
+
if (openSessionId !== null) {
|
|
509
|
+
const turnState = readTurnState(currentDb);
|
|
510
|
+
if (turnState !== null && turnState.scope_session_id === openSessionId) {
|
|
511
|
+
const turnKey = `${openSessionId}:${turnState.current_actor}`;
|
|
512
|
+
if (turnKey !== lastTurnKey) {
|
|
513
|
+
events.push({
|
|
514
|
+
kind: "turn_transition",
|
|
515
|
+
session_id: openSessionId,
|
|
516
|
+
from_actor: lastTurnActor,
|
|
517
|
+
to_actor: turnState.current_actor,
|
|
518
|
+
observed_at_tick: tick,
|
|
519
|
+
});
|
|
520
|
+
lastTurnKey = turnKey;
|
|
521
|
+
lastTurnActor = turnState.current_actor;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// --- reaction_window (opened / closed) ---
|
|
527
|
+
const nextReactionWindows = reactionSnapshot(currentDb, currentActorId);
|
|
528
|
+
for (const [pendingId, entry] of nextReactionWindows) {
|
|
529
|
+
if (lastReactionWindows.has(pendingId)) continue;
|
|
530
|
+
events.push({
|
|
531
|
+
kind: "reaction_window",
|
|
532
|
+
window_kind: entry.windowKind,
|
|
533
|
+
phase: "opened",
|
|
534
|
+
pending_id: pendingId,
|
|
535
|
+
originating_action_id: entry.originatingActionId,
|
|
536
|
+
originating_actor: entry.originatingActor,
|
|
537
|
+
observed_at_tick: tick,
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
for (const [pendingId, entry] of lastReactionWindows) {
|
|
541
|
+
if (nextReactionWindows.has(pendingId)) continue;
|
|
542
|
+
events.push({
|
|
543
|
+
kind: "reaction_window",
|
|
544
|
+
window_kind: entry.windowKind,
|
|
545
|
+
phase: "closed",
|
|
546
|
+
pending_id: pendingId,
|
|
547
|
+
originating_action_id: entry.originatingActionId,
|
|
548
|
+
originating_actor: entry.originatingActor,
|
|
549
|
+
observed_at_tick: tick,
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
lastReactionWindows = nextReactionWindows;
|
|
553
|
+
|
|
554
|
+
// --- self_hit / self_resource_spent / xp_gained / dealt_hit
|
|
555
|
+
// (semantic dedupe by event_id — never row/handle identity,
|
|
556
|
+
// matching `delta-tracker.ts`'s own fingerprint discipline).
|
|
557
|
+
// Classification is the resource STEM, never a bundle prefix:
|
|
558
|
+
// hp/temp_hp stay combat hits; own `xp` gains are awards; every
|
|
559
|
+
// other own negative delta is a spend. A foreign `xp_value`
|
|
560
|
+
// latch is another body's sheet and is never pushed (sk-0m4.15).
|
|
561
|
+
//
|
|
562
|
+
// A spend is only a spend when its source is the actor: an own
|
|
563
|
+
// non-hit-point delta whose `source_entity_ref` names ANOTHER entity (a
|
|
564
|
+
// DM sheet write, a drain) is not pushed at all. An empty source and the
|
|
565
|
+
// actor's own id keep the event. `self_hit` is never filtered this way.
|
|
566
|
+
// The source is the frame's `Actor` binding, which a
|
|
567
|
+
// `ForEachActorInRadius` body rebinds to each target: a radius-applied
|
|
568
|
+
// delta reads as self-caused here and stays.
|
|
569
|
+
//
|
|
570
|
+
// Nothing here is pushed directly. Resource and condition events of
|
|
571
|
+
// one drain are collected, then sorted together oldest-first by event
|
|
572
|
+
// id (module header) — the two logs draw from one id space. ---
|
|
573
|
+
const deltaEvents: Array<{ eventId: string; order: number; event: WorldEvent }> = [];
|
|
574
|
+
const collect = (eventId: string, event: WorldEvent): void => {
|
|
575
|
+
deltaEvents.push({ eventId, order: deltaEvents.length, event });
|
|
576
|
+
};
|
|
577
|
+
// Resolved at most once per drain, and only when a new `self_hit`
|
|
578
|
+
// carries an attacker other than the actor.
|
|
579
|
+
let entityNames: Map<string, string> | undefined;
|
|
580
|
+
const attackerNameOf = (attackerId: string | undefined): string | undefined => {
|
|
581
|
+
if (attackerId === undefined || attackerId === entityIdText) return undefined;
|
|
582
|
+
entityNames ??= buildEntityNameMap(currentDb);
|
|
583
|
+
const name = entityNames.get(attackerId);
|
|
584
|
+
return name === undefined || name.length === 0 ? undefined : name;
|
|
585
|
+
};
|
|
586
|
+
const nextDeltas = ownNegativeResourceDeltaEventIds(currentDb, currentActorId);
|
|
587
|
+
const nextXpGains = ownXpGainEventIds(currentDb, currentActorId);
|
|
588
|
+
const nextDealt = dealtNegativeResourceDeltaEventIds(currentDb, currentActorId);
|
|
589
|
+
const nextSeen = new Set<string>();
|
|
590
|
+
for (const [eventId, delta] of nextDeltas) {
|
|
591
|
+
nextSeen.add(eventId);
|
|
592
|
+
if (seenResourceDeltaEventIds.has(eventId)) continue;
|
|
593
|
+
if (isHitPointResource(delta.resourceId)) {
|
|
594
|
+
const attackerName = attackerNameOf(delta.attackerId);
|
|
595
|
+
collect(eventId, {
|
|
596
|
+
kind: "self_hit",
|
|
597
|
+
resource_id: delta.resourceId,
|
|
598
|
+
amount: delta.amount,
|
|
599
|
+
new_value: delta.newValue,
|
|
600
|
+
attack_kind: delta.templateId,
|
|
601
|
+
// ADR-0093 B2: populated from `event_resource_delta.source_entity_ref`
|
|
602
|
+
// when the server attributed one; `null` only for a genuinely
|
|
603
|
+
// ambient/ownerless delta (module header).
|
|
604
|
+
attacker_id: delta.attackerId ?? null,
|
|
605
|
+
...(attackerName !== undefined ? { attacker_name: attackerName } : {}),
|
|
606
|
+
observed_at_tick: tick,
|
|
607
|
+
});
|
|
608
|
+
} else {
|
|
609
|
+
// Caused by another entity: not this actor's spend. Skipped AFTER
|
|
610
|
+
// the seen-set insert above, so it is never reconsidered.
|
|
611
|
+
if (
|
|
612
|
+
delta.attackerId !== undefined &&
|
|
613
|
+
entityIdText !== undefined &&
|
|
614
|
+
delta.attackerId !== entityIdText
|
|
615
|
+
) {
|
|
616
|
+
continue;
|
|
617
|
+
}
|
|
618
|
+
collect(eventId, {
|
|
619
|
+
kind: "self_resource_spent",
|
|
620
|
+
resource_id: delta.resourceId,
|
|
621
|
+
amount: delta.amount,
|
|
622
|
+
new_value: delta.newValue,
|
|
623
|
+
source: delta.templateId,
|
|
624
|
+
observed_at_tick: tick,
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
for (const [eventId, gain] of nextXpGains) {
|
|
629
|
+
nextSeen.add(eventId);
|
|
630
|
+
if (seenResourceDeltaEventIds.has(eventId)) continue;
|
|
631
|
+
collect(eventId, {
|
|
632
|
+
kind: "xp_gained",
|
|
633
|
+
amount: gain.amount,
|
|
634
|
+
new_value: gain.newValue,
|
|
635
|
+
observed_at_tick: tick,
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
for (const [eventId, dealt] of nextDealt) {
|
|
639
|
+
nextSeen.add(eventId);
|
|
640
|
+
if (seenResourceDeltaEventIds.has(eventId)) continue;
|
|
641
|
+
collect(eventId, {
|
|
642
|
+
kind: "dealt_hit",
|
|
643
|
+
target_name: dealt.targetName,
|
|
644
|
+
amount: dealt.amount,
|
|
645
|
+
attacker_id: dealt.attackerId ?? null,
|
|
646
|
+
observed_at_tick: tick,
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
seenResourceDeltaEventIds = nextSeen;
|
|
650
|
+
|
|
651
|
+
// --- self_condition_changed / dealt_condition (K-B Part B — same
|
|
652
|
+
// semantic dedupe-by-event_id discipline as the resource-delta block
|
|
653
|
+
// above, in its own Set since a condition_delta event_id and a
|
|
654
|
+
// resource_delta event_id are drawn from the same world_event id
|
|
655
|
+
// space and must not collide in one shared Set). ---
|
|
656
|
+
const nextConditionChanges = ownConditionDeltaEventIds(currentDb, currentActorId);
|
|
657
|
+
const nextDealtConditions = dealtConditionDeltaEventIds(currentDb, currentActorId);
|
|
658
|
+
const nextConditionSeen = new Set<string>();
|
|
659
|
+
for (const [eventId, change] of nextConditionChanges) {
|
|
660
|
+
nextConditionSeen.add(eventId);
|
|
661
|
+
if (seenConditionDeltaEventIds.has(eventId)) continue;
|
|
662
|
+
collect(eventId, {
|
|
663
|
+
kind: "self_condition_changed",
|
|
664
|
+
condition_id: change.conditionId,
|
|
665
|
+
applied: change.applied,
|
|
666
|
+
source_entity_id: change.sourceEntityId ?? null,
|
|
667
|
+
observed_at_tick: tick,
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
for (const [eventId, dealt] of nextDealtConditions) {
|
|
671
|
+
nextConditionSeen.add(eventId);
|
|
672
|
+
if (seenConditionDeltaEventIds.has(eventId)) continue;
|
|
673
|
+
collect(eventId, {
|
|
674
|
+
kind: "dealt_condition",
|
|
675
|
+
target_name: dealt.targetName,
|
|
676
|
+
condition_id: dealt.conditionId,
|
|
677
|
+
applied: dealt.applied,
|
|
678
|
+
source_entity_id: dealt.sourceEntityId ?? null,
|
|
679
|
+
observed_at_tick: tick,
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
seenConditionDeltaEventIds = nextConditionSeen;
|
|
683
|
+
|
|
684
|
+
// Oldest-first by event id, with the SAME numeric-aware comparator the
|
|
685
|
+
// readers sort by (inverted). Ids are compared as text on purpose: a
|
|
686
|
+
// host may hand over non-numeric ids. Equal ids (one resource row and
|
|
687
|
+
// one condition row of the same world event) keep collection order.
|
|
688
|
+
deltaEvents.sort(
|
|
689
|
+
(a, b) =>
|
|
690
|
+
a.eventId.localeCompare(b.eventId, undefined, { numeric: true }) || a.order - b.order,
|
|
691
|
+
);
|
|
692
|
+
for (const entry of deltaEvents) events.push(entry.event);
|
|
693
|
+
|
|
694
|
+
// --- advancement_available (watched factored manipulation action,
|
|
695
|
+
// absent -> present; module header + `watchedAdvancementSnapshot`) ---
|
|
696
|
+
const nextAdvancementActions = watchedAdvancementSnapshot(currentDb, currentActorId);
|
|
697
|
+
for (const [actionId, displayName] of nextAdvancementActions) {
|
|
698
|
+
if (lastAdvancementActions.has(actionId)) continue;
|
|
699
|
+
events.push({
|
|
700
|
+
kind: "advancement_available",
|
|
701
|
+
action_id: actionId,
|
|
702
|
+
display_name: displayName,
|
|
703
|
+
observed_at_tick: tick,
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
lastAdvancementActions = nextAdvancementActions;
|
|
707
|
+
|
|
708
|
+
// --- speech_heard (a NEW text-carrying trace becomes perceivable;
|
|
709
|
+
// semantic dedupe by trace_id, same discipline as self_hit's
|
|
710
|
+
// event_id dedupe — a trace that stays visible across calls is never
|
|
711
|
+
// re-reported) ---
|
|
712
|
+
const nextTextTraces = textCarryingTraceSnapshot(currentDb);
|
|
713
|
+
const nextSeenTraceIds = new Set<string>();
|
|
714
|
+
// Resolved at most once per drain, and only if a new trace actually
|
|
715
|
+
// needs a speaker name — most drains carry none.
|
|
716
|
+
const hearerActorId = currentActorId;
|
|
717
|
+
let perceivedNames: Map<string, string> | undefined;
|
|
718
|
+
const nameOf = (entityId: string | null): string | null => {
|
|
719
|
+
if (entityId === null) return null;
|
|
720
|
+
perceivedNames ??= perceivedActorNames(currentDb, hearerActorId, entityIdText);
|
|
721
|
+
const name = perceivedNames.get(entityId);
|
|
722
|
+
return name === undefined || name.length === 0 ? null : name;
|
|
723
|
+
};
|
|
724
|
+
for (const [traceId, row] of nextTextTraces) {
|
|
725
|
+
nextSeenTraceIds.add(traceId);
|
|
726
|
+
if (seenTextTraceIds.has(traceId)) continue;
|
|
727
|
+
// sk-dms.17: the actor's OWN utterance is not world news. The
|
|
728
|
+
// discriminator here is structural (a new text-carrying row in the
|
|
729
|
+
// actor's own `visible_trace`) and therefore also matches what the
|
|
730
|
+
// actor just said itself — but the act's own result already echoes
|
|
731
|
+
// that back (`conversation.spoken_by_player`), and the 🌍 register is
|
|
732
|
+
// the world speaking, not the actor. Skipped AFTER the seen-set
|
|
733
|
+
// insert above, so it is never re-considered on a later drain.
|
|
734
|
+
if (
|
|
735
|
+
row.inscribed_by !== null &&
|
|
736
|
+
entityIdText !== undefined &&
|
|
737
|
+
row.inscribed_by === entityIdText
|
|
738
|
+
) {
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
const payload = row.payload as { text: string; action_id?: unknown; addressee?: unknown };
|
|
742
|
+
const actionId =
|
|
743
|
+
typeof payload.action_id === "string" && payload.action_id.length > 0
|
|
744
|
+
? payload.action_id
|
|
745
|
+
: undefined;
|
|
746
|
+
// `srd/speech`'s schema (`speech-and-traces.md`) declares `addressee`
|
|
747
|
+
// as an optional integer — present only on addressee-anchored chains
|
|
748
|
+
// (`srd/speak-to`, telepathy), absent on broadcast/self-shaped speech.
|
|
749
|
+
// A present-but-non-finite value is treated the same as absent rather
|
|
750
|
+
// than surfaced as a bogus id.
|
|
751
|
+
const addressee =
|
|
752
|
+
typeof payload.addressee === "number" && Number.isFinite(payload.addressee)
|
|
753
|
+
? payload.addressee
|
|
754
|
+
: undefined;
|
|
755
|
+
events.push({
|
|
756
|
+
kind: "speech_heard",
|
|
757
|
+
trace_id: traceId,
|
|
758
|
+
trace_kind: row.kind,
|
|
759
|
+
// `hasTextPayload` already narrowed `row.payload` to `{ text: string }`
|
|
760
|
+
// at snapshot time; re-asserted here since the Map value type is
|
|
761
|
+
// the wider `TraceRow`.
|
|
762
|
+
text: payload.text,
|
|
763
|
+
...(actionId !== undefined ? { action_id: actionId } : {}),
|
|
764
|
+
...(row.response === undefined ? {} : { response: row.response }),
|
|
765
|
+
inscribed_by: row.inscribed_by,
|
|
766
|
+
inscribed_by_name: nameOf(row.inscribed_by),
|
|
767
|
+
// Same epistemics as `inscribed_by_name`: a name only when the
|
|
768
|
+
// hearer currently perceives the addressee (`nameOf` is the SAME
|
|
769
|
+
// scene-projection lookup, just keyed by the addressee's id).
|
|
770
|
+
...(addressee !== undefined
|
|
771
|
+
? { addressee, addressee_name: nameOf(String(addressee)) }
|
|
772
|
+
: {}),
|
|
773
|
+
inscribed_at: row.inscribed_at,
|
|
774
|
+
location_kind: row.location_kind,
|
|
775
|
+
location_ref: row.location_ref,
|
|
776
|
+
observed_at_tick: tick,
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
seenTextTraceIds = nextSeenTraceIds;
|
|
780
|
+
|
|
781
|
+
// --- trace_addressed (a NEW addressed non-text trace becomes
|
|
782
|
+
// perceivable) — the same dedupe, the same own-inscription skip, and
|
|
783
|
+
// the same `nameOf` epistemics as speech_heard above. ---
|
|
784
|
+
const nextAddressedTraces = addressedTraceSnapshot(currentDb);
|
|
785
|
+
const nextSeenAddressedIds = new Set<string>();
|
|
786
|
+
for (const [traceId, row] of nextAddressedTraces) {
|
|
787
|
+
nextSeenAddressedIds.add(traceId);
|
|
788
|
+
if (seenAddressedTraceIds.has(traceId)) continue;
|
|
789
|
+
// Same sk-dms.17 rule as speech_heard: the actor's own inscription
|
|
790
|
+
// is not world news, skipped AFTER the seen-set insert so it is
|
|
791
|
+
// never reconsidered on a later drain.
|
|
792
|
+
if (
|
|
793
|
+
row.inscribed_by !== null &&
|
|
794
|
+
entityIdText !== undefined &&
|
|
795
|
+
row.inscribed_by === entityIdText
|
|
796
|
+
) {
|
|
797
|
+
continue;
|
|
798
|
+
}
|
|
799
|
+
const payload = row.payload as Record<string, unknown>;
|
|
800
|
+
const addressee = payload.addressee as number;
|
|
801
|
+
// sk-jay0.1.8.63.40: `label` is the one well-known, generic payload
|
|
802
|
+
// key this builder reads out of an otherwise payload-blind trace
|
|
803
|
+
// (`@spellkave/world-events`'s `TraceAddressedEvent.label` doc
|
|
804
|
+
// comment) — extracted the same defensive way `speech_heard`'s
|
|
805
|
+
// `action_id` already is, just above. `addressed_at_self` is a
|
|
806
|
+
// structural id comparison, never a payload read: this actor IS the
|
|
807
|
+
// addressee exactly when the resolved current-actor id matches.
|
|
808
|
+
const label =
|
|
809
|
+
typeof payload.label === "string" && payload.label.length > 0 ? payload.label : undefined;
|
|
810
|
+
events.push({
|
|
811
|
+
kind: "trace_addressed",
|
|
812
|
+
trace_id: traceId,
|
|
813
|
+
trace_kind: row.kind,
|
|
814
|
+
payload,
|
|
815
|
+
inscribed_by: row.inscribed_by,
|
|
816
|
+
inscribed_by_name: nameOf(row.inscribed_by),
|
|
817
|
+
inscribed_at: row.inscribed_at,
|
|
818
|
+
addressee,
|
|
819
|
+
addressee_name: nameOf(String(addressee)),
|
|
820
|
+
addressed_at_self: entityIdText !== undefined && String(addressee) === entityIdText,
|
|
821
|
+
...(label !== undefined ? { label } : {}),
|
|
822
|
+
location_kind: row.location_kind,
|
|
823
|
+
location_ref: row.location_ref,
|
|
824
|
+
observed_at_tick: tick,
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
seenAddressedTraceIds = nextSeenAddressedIds;
|
|
828
|
+
|
|
829
|
+
// --- trace_departure (a NEW departure trace becomes perceivable) —
|
|
830
|
+
// the same dedupe, the same own-inscription skip, and the same
|
|
831
|
+
// `nameOf` epistemics as the two kinds above. The mover's name is a
|
|
832
|
+
// name only when this actor can still perceive them, which after a
|
|
833
|
+
// departure is usually not the case: `null` is the ordinary reading
|
|
834
|
+
// here, not a failure. ---
|
|
835
|
+
const nextDepartureTraces = departureTraceSnapshot(currentDb);
|
|
836
|
+
const nextSeenDepartureIds = new Set<string>();
|
|
837
|
+
for (const [traceId, row] of nextDepartureTraces) {
|
|
838
|
+
nextSeenDepartureIds.add(traceId);
|
|
839
|
+
if (seenDepartureTraceIds.has(traceId)) continue;
|
|
840
|
+
// Same sk-dms.17 rule: an actor's own departure is not world news
|
|
841
|
+
// to itself, skipped AFTER the seen-set insert. (The kernel also
|
|
842
|
+
// declines to record the mover as a perceiver of its own trace, so
|
|
843
|
+
// this is a second, independent guard rather than the only one.)
|
|
844
|
+
if (
|
|
845
|
+
row.inscribed_by !== null &&
|
|
846
|
+
entityIdText !== undefined &&
|
|
847
|
+
row.inscribed_by === entityIdText
|
|
848
|
+
) {
|
|
849
|
+
continue;
|
|
850
|
+
}
|
|
851
|
+
const payload = row.payload as {
|
|
852
|
+
mover: number;
|
|
853
|
+
via_place_edge_id: string;
|
|
854
|
+
exit_label?: unknown;
|
|
855
|
+
};
|
|
856
|
+
events.push({
|
|
857
|
+
kind: "trace_departure",
|
|
858
|
+
trace_id: traceId,
|
|
859
|
+
trace_kind: row.kind,
|
|
860
|
+
mover: payload.mover,
|
|
861
|
+
mover_name: nameOf(String(payload.mover)),
|
|
862
|
+
from_place_node_id: row.location_ref,
|
|
863
|
+
via_place_edge_id: payload.via_place_edge_id,
|
|
864
|
+
// Payload-declared but not part of the discriminator: an
|
|
865
|
+
// absent or non-string label reads as "no label", never as a
|
|
866
|
+
// stringified object.
|
|
867
|
+
exit_label: typeof payload.exit_label === "string" ? payload.exit_label : "",
|
|
868
|
+
inscribed_at: row.inscribed_at,
|
|
869
|
+
location_kind: row.location_kind,
|
|
870
|
+
location_ref: row.location_ref,
|
|
871
|
+
observed_at_tick: tick,
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
seenDepartureTraceIds = nextSeenDepartureIds;
|
|
875
|
+
|
|
876
|
+
if (deferredClosed !== undefined) events.push(deferredClosed);
|
|
877
|
+
return events;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
return {
|
|
881
|
+
diffAndAdvance(): WorldEvent[] {
|
|
882
|
+
const fresh = computeNewEvents();
|
|
883
|
+
if (carry.length === 0) return fresh;
|
|
884
|
+
const combined = [...carry, ...fresh];
|
|
885
|
+
carry = [];
|
|
886
|
+
return combined;
|
|
887
|
+
},
|
|
888
|
+
takeWhere(predicate: (event: WorldEvent) => boolean): WorldEvent[] {
|
|
889
|
+
const fresh = computeNewEvents();
|
|
890
|
+
const combined = carry.length === 0 ? fresh : [...carry, ...fresh];
|
|
891
|
+
const matched: WorldEvent[] = [];
|
|
892
|
+
const unmatched: WorldEvent[] = [];
|
|
893
|
+
for (const event of combined) {
|
|
894
|
+
if (predicate(event)) matched.push(event);
|
|
895
|
+
else unmatched.push(event);
|
|
896
|
+
}
|
|
897
|
+
carry = unmatched;
|
|
898
|
+
return matched;
|
|
899
|
+
},
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
/**
|
|
904
|
+
* sk-0m4.74: the CONSEQUENCE predicate — a `WorldEvent` this actor's OWN act
|
|
905
|
+
* deterministically caused, in the SAME reducer transaction as that act, as
|
|
906
|
+
* opposed to an Outcome (the reducer's own `receipt`/`observation`, already
|
|
907
|
+
* returned synchronously by the dispatching tool) or a Reaction (another
|
|
908
|
+
* agent's later action, e.g. a bounded `communicate` reply window,
|
|
909
|
+
* sk-0m4.73). Callers take with `builder.takeWhere((e) =>
|
|
910
|
+
* isCausedByActor(e, actorEntityId, tick))` right after a successful dispatch
|
|
911
|
+
* — never on a rejected/unobserved one, since nothing was caused.
|
|
912
|
+
*
|
|
913
|
+
* `actorEntityId` is the RESOLVED entity id string (module header's
|
|
914
|
+
* `resolveEntityId` discipline — `attacker_id`/`originating_actor`/
|
|
915
|
+
* `participants` are all resolved entity ids, never a manifest actor ref).
|
|
916
|
+
* `tick` is the act's own tick; when the caller cannot cheaply learn that
|
|
917
|
+
* (this bead's assumption, `tools.ts`'s `takeConsequences`), the CURRENT
|
|
918
|
+
* tick read from the db right after the outcome stands in for it — correct
|
|
919
|
+
* under ordinary turn-based single-actor-per-transaction semantics, where
|
|
920
|
+
* "what changed in this tick, for this actor" and "what my just-dispatched
|
|
921
|
+
* act changed" coincide.
|
|
922
|
+
*
|
|
923
|
+
* Gated to `event.observed_at_tick === tick` for EVERY kind — same-tick
|
|
924
|
+
* alone is not sufficient for a kind another actor can also cause (a
|
|
925
|
+
* same-tick `self_hit` from someone else's attack), so every arm below
|
|
926
|
+
* layers a same-actor check on top; the kinds this predicate NEVER accepts
|
|
927
|
+
* (`self_hit`, `turn_transition`, `speech_heard`, `advancement_available`,
|
|
928
|
+
* a `reaction_window`/`session_lifecycle` `"closed"` phase) stay honest
|
|
929
|
+
* pushes even at the right tick, because tick alone cannot attribute them to
|
|
930
|
+
* THIS actor's own act (module header's "loss is impossible" carry
|
|
931
|
+
* semantics: whatever this predicate rejects is never dropped, only
|
|
932
|
+
* buffered for a later `pushes` drain).
|
|
933
|
+
*
|
|
934
|
+
* - `dealt_hit`: `attacker_id === actorEntityId` — in practice always true
|
|
935
|
+
* for THIS actor's own builder (see {@link DealtHitEvent}'s doc comment),
|
|
936
|
+
* kept explicit rather than assumed.
|
|
937
|
+
* - `dealt_condition`: `source_entity_id === actorEntityId` — same shape as
|
|
938
|
+
* `dealt_hit`, one level removed (a condition applied/removed instead of
|
|
939
|
+
* a resource delta). `self_condition_changed` is NEVER accepted (like
|
|
940
|
+
* `self_hit`): a same-tick condition change on this actor's OWN body can
|
|
941
|
+
* just as easily be another actor's attack landing a status effect.
|
|
942
|
+
* - `self_resource_spent` / `xp_gained`: both are already scoped to the
|
|
943
|
+
* CONNECTED actor's own pool by construction (every other actor's pool
|
|
944
|
+
* changes are invisible to this builder) — no further identity check
|
|
945
|
+
* needed, only the tick gate. Since 2026-11-12 the builder does not emit
|
|
946
|
+
* a `self_resource_spent` another entity caused, so such a change is
|
|
947
|
+
* neither a consequence nor a push.
|
|
948
|
+
* - `reaction_window`, `phase === "opened"`: `originating_actor ===
|
|
949
|
+
* actorEntityId` — a window THIS actor's own act just opened (for itself
|
|
950
|
+
* or another eligible reactor), not one another actor's act opened that
|
|
951
|
+
* merely happens to list this actor as reactor.
|
|
952
|
+
* - `session_lifecycle`, `phase === "opened"`: `opened_by ===
|
|
953
|
+
* actorEntityId` — this actor's own act opened the session, not a session
|
|
954
|
+
* another actor's act opened that this actor was merely seated into.
|
|
955
|
+
*/
|
|
956
|
+
export function isCausedByActor(event: WorldEvent, actorEntityId: string, tick: number): boolean {
|
|
957
|
+
return runtimeIsCausedByActor(event, actorEntityId, tick);
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* Entity id -> perceived display name for every actor CO-LOCATED with the
|
|
962
|
+
* hearer. Deliberately the scene projection and nothing wider: `readCurrentScene`
|
|
963
|
+
* is already the perception-gated surface the player's own `spellkave_scene`
|
|
964
|
+
* reads, so a name reaches an event only when the hearer could have seen whose
|
|
965
|
+
* mouth it came out of. A raw `actor`-table lookup would name speakers the
|
|
966
|
+
* hearer has never perceived. `self_hit` does not hold that line: its
|
|
967
|
+
* `attacker_id` and `attacker_name` are disclosed ungated, and suppression is
|
|
968
|
+
* the deferred ADR-0082 D-6 item (module header, T29).
|
|
969
|
+
*/
|
|
970
|
+
function perceivedActorNames(
|
|
971
|
+
db: SnapshotDb,
|
|
972
|
+
actorId: EntityRef,
|
|
973
|
+
entityIdText: string | undefined,
|
|
974
|
+
): Map<string, string> {
|
|
975
|
+
const names = new Map<string, string>();
|
|
976
|
+
const scene = readCurrentScene(db, actorId);
|
|
977
|
+
if (scene !== null) {
|
|
978
|
+
for (const target of scene.targets) {
|
|
979
|
+
if (target.kind !== "actor") continue;
|
|
980
|
+
const name = target.detail.name;
|
|
981
|
+
if (typeof name === "string" && name.length > 0) names.set(String(target.id), name);
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
// The one widening: the roster of the hearer's OWN active session. On a
|
|
985
|
+
// tactical grid every participant stands on a cell of its own, so nobody
|
|
986
|
+
// is "co-located" and the scene names no one — the creature shooting at
|
|
987
|
+
// the hearer from two cells away read as "an unseen figure" (seen live,
|
|
988
|
+
// sk-jay0.1.8.63.40). `visible_session_participant` is already
|
|
989
|
+
// participant-scoped, and `spellkave_session_status` already tells this
|
|
990
|
+
// same actor these same names, so this names nobody the hearer has not
|
|
991
|
+
// been shown. Co-located names win; the roster only fills the gaps.
|
|
992
|
+
const mine =
|
|
993
|
+
entityIdText === undefined ? null : readActiveSessionWithRosterForParticipant(db, entityIdText);
|
|
994
|
+
if (mine !== null) {
|
|
995
|
+
const entityNames = buildEntityNameMap(db);
|
|
996
|
+
for (const participant of mine.participants) {
|
|
997
|
+
if (names.has(participant.actorId) || participant.actorId === entityIdText) continue;
|
|
998
|
+
const name = entityNames.get(participant.actorId);
|
|
999
|
+
if (name !== undefined && name.length > 0) names.set(participant.actorId, name);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
return names;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
/** Type guard: `payload` is a JSON object carrying a string `text` key — the
|
|
1006
|
+
* kernel-level `carry_dispatch_text` discriminator (ADR-0021), never a
|
|
1007
|
+
* `kind`/`bundle_id` check (module header). */
|
|
1008
|
+
function hasTextPayload(payload: unknown): payload is { text: string } {
|
|
1009
|
+
return (
|
|
1010
|
+
payload !== null &&
|
|
1011
|
+
typeof payload === "object" &&
|
|
1012
|
+
typeof (payload as { text?: unknown }).text === "string"
|
|
1013
|
+
);
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
/** Type guard: `payload` is a JSON object carrying a finite numeric `mover`
|
|
1017
|
+
* AND a string `via_place_edge_id` — the kernel-written structural keys of a
|
|
1018
|
+
* `core/departure` row, the third sibling of `hasTextPayload` /
|
|
1019
|
+
* `hasAddresseePayload`. BOTH keys are required: one alone is not this
|
|
1020
|
+
* contract, and a payload that merely happens to name a mover must not be
|
|
1021
|
+
* reported as a departure with an empty exit. */
|
|
1022
|
+
function hasDeparturePayload(
|
|
1023
|
+
payload: unknown,
|
|
1024
|
+
): payload is { mover: number; via_place_edge_id: string } {
|
|
1025
|
+
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) return false;
|
|
1026
|
+
const { mover, via_place_edge_id: edge } = payload as {
|
|
1027
|
+
mover?: unknown;
|
|
1028
|
+
via_place_edge_id?: unknown;
|
|
1029
|
+
};
|
|
1030
|
+
return typeof mover === "number" && Number.isFinite(mover) && typeof edge === "string";
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
/** Type guard: `payload` is a JSON object carrying a finite numeric
|
|
1034
|
+
* `addressee` key — the kernel-level `carry_addressee` discriminator, the
|
|
1035
|
+
* structural sibling of `hasTextPayload`. A present-but-non-finite value is
|
|
1036
|
+
* treated as absent rather than surfaced as a bogus id. Arrays are rejected:
|
|
1037
|
+
* an inscribed payload is an object or it is not this contract. */
|
|
1038
|
+
function hasAddresseePayload(payload: unknown): payload is { addressee: number } {
|
|
1039
|
+
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) return false;
|
|
1040
|
+
const addressee = (payload as { addressee?: unknown }).addressee;
|
|
1041
|
+
return typeof addressee === "number" && Number.isFinite(addressee);
|
|
1042
|
+
}
|