Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | 1x 1x 1x 1x 1x 1x 1x 10x 10x 1x 12x 14x 14x 14x 14x 14x 10x 4x 10x 4x 4x 4x 4x 4x 4x 1x 4x 4x 6x 4x 14x 8x 6x 5x 4x 4x 4x 4x 1x 2x 2x 4x 2x 5x 1x | import { EVENT_SWITCH_SCENE, MAX_NESTED_SCRIPT_DEPTH } from "consts";
import uniqBy from "lodash/uniqBy";
import {
ActorDirection,
ShowConnectionsSetting,
} from "shared/lib/resources/types";
import { optimiseScriptValue } from "shared/lib/scriptValue/helpers";
import { ensureScriptValue } from "shared/lib/scriptValue/types";
// eslint-disable-next-line no-restricted-globals
const workerCtx: Worker = self as unknown as Worker;
type ConnectionType = "actor" | "trigger" | "scene";
export interface ConnectionScriptEvent {
id: string;
command: string;
sceneId?: string;
customEventId?: string;
x?: unknown;
y?: unknown;
direction?: ActorDirection;
commented?: boolean;
children?: string[][];
}
export interface ConnectionScriptSource {
id: string;
scripts: string[][];
overrides?: Record<
string,
{
sceneId?: string;
customEventId?: string;
x?: unknown;
y?: unknown;
direction?: ActorDirection;
}
>;
}
export interface ConnectionScene extends ConnectionScriptSource {
actors: ConnectionScriptSource[];
triggers: ConnectionScriptSource[];
}
export interface ConnectionsWorkerRequest {
scenes: ConnectionScene[];
sceneIds: string[];
showConnections: ShowConnectionsSetting;
selectedSceneId: string;
events: Record<string, ConnectionScriptEvent>;
customEvents: Record<string, string[]>;
}
export interface ConnectionsWorkerResult {
connections: SceneTransition[];
}
export interface SceneTransition {
type: ConnectionType;
eventId: string;
fromSceneId: string;
toSceneId: string;
entityId: string;
toX: number;
toY: number;
direction?: ActorDirection;
}
interface WalkOptions {
depth: number;
overrides?: ConnectionScriptSource["overrides"];
visitedCustomEvents: Set<string>;
}
const defaultCoord = {
type: "number",
value: 0,
} as const;
const eventDestinationCoord = (value: unknown): number => {
const scriptValue = optimiseScriptValue(
ensureScriptValue(value, defaultCoord),
);
return scriptValue.type === "number" ? scriptValue.value : 0;
};
const walkScript = (
script: string[],
events: ConnectionsWorkerRequest["events"],
customEvents: ConnectionsWorkerRequest["customEvents"],
options: WalkOptions,
callback: (
event: ConnectionScriptEvent,
resolvedArgs: {
sceneId?: string;
x?: unknown;
y?: unknown;
direction?: ActorDirection;
},
) => void,
) => {
for (const eventId of script) {
const event = events[eventId];
Iif (!event || event.commented) {
continue;
}
const override = options.overrides?.[event.id];
callback(event, {
sceneId: override?.sceneId ?? event.sceneId,
x: override?.x ?? event.x,
y: override?.y ?? event.y,
direction: override?.direction ?? event.direction,
});
if (event.command !== "EVENT_CALL_CUSTOM_EVENT") {
event.children?.forEach((child) =>
walkScript(child, events, customEvents, options, callback),
);
continue;
}
const customEventId = override?.customEventId ?? event.customEventId;
const customScript = customEventId && customEvents[customEventId];
Eif (
customScript &&
options.depth >= 0 &&
!options.visitedCustomEvents.has(customEventId)
) {
const visitedCustomEvents = new Set(options.visitedCustomEvents);
visitedCustomEvents.add(customEventId);
walkScript(
customScript,
events,
customEvents,
{
...options,
depth: options.depth - 1,
visitedCustomEvents,
},
callback,
);
}
}
};
const getSceneConnections = (
request: ConnectionsWorkerRequest,
scene: ConnectionScene,
validSceneIds: Set<string>,
) => {
const connections: SceneTransition[] = [];
const walkSource = (source: ConnectionScriptSource, type: ConnectionType) => {
source.scripts.forEach((script) =>
walkScript(
script,
request.events,
request.customEvents,
{
depth: MAX_NESTED_SCRIPT_DEPTH,
overrides: source.overrides,
visitedCustomEvents: new Set(),
},
(event, destination) => {
if (
event.command !== EVENT_SWITCH_SCENE ||
!destination.sceneId ||
!validSceneIds.has(destination.sceneId)
) {
return;
}
if (
request.showConnections === "all" ||
scene.id === request.selectedSceneId ||
destination.sceneId === request.selectedSceneId
) {
connections.push({
type,
eventId: event.id,
fromSceneId: scene.id,
toSceneId: destination.sceneId,
entityId: type === "scene" ? "" : source.id,
toX: eventDestinationCoord(destination.x),
toY: eventDestinationCoord(destination.y),
direction: destination.direction,
});
}
},
),
);
};
walkSource(scene, "scene");
scene.actors.forEach((actor) => walkSource(actor, "actor"));
scene.triggers.forEach((trigger) => walkSource(trigger, "trigger"));
return connections;
};
export const calculateConnections = (request: ConnectionsWorkerRequest) => {
const validSceneIds = new Set(request.sceneIds);
const connections = request.scenes.flatMap((scene) =>
getSceneConnections(request, scene, validSceneIds),
);
return uniqBy(
connections,
(connection) =>
`${connection.fromSceneId}_${connection.entityId}_${connection.eventId}`,
);
};
workerCtx.onmessage = (evt) => {
const request = evt.data as ConnectionsWorkerRequest;
// Multiple calls to the same custom event from one source should not draw
// multiple overlapping lines.
workerCtx.postMessage({
connections: calculateConnections(request),
} satisfies ConnectionsWorkerResult);
};
|