@adatechnology/game-urban 0.5.3 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { DeepPartial, UtilityNetwork, UtilityConnection } from '@adatechnology/game-contracts';
1
+ import { DeepPartial, UtilityNetwork, UtilityConnection, DestructibleSpec, GroundExcavation } from '@adatechnology/game-contracts';
2
2
  import { ap as PoleSlots, ao as PoleSlotBand, m as PoleDef, am as PlanRect, C as CityPlanInput, Q as DistributionSpec, a7 as LampState, a5 as LampShares } from '../types-mY6VwZgN.js';
3
3
  export { O as DistributionPoleDef } from '../types-mY6VwZgN.js';
4
4
  import '@adatechnology/game-floorplan';
@@ -165,6 +165,16 @@ type NetworkRules = {
165
165
  readonly lateralOverGallery: number;
166
166
  /** Longest sewer main between two manholes. */
167
167
  readonly manholeSpacing: number;
168
+ /** A pipe that turns more than this, degrees, or meets two others, is a manhole (a PV). */
169
+ readonly turnTolerance: number;
170
+ /** A house lateral leaves the street's main at a right angle, within this many degrees. */
171
+ readonly branchMaxSkew: number;
172
+ };
173
+ readonly storm: {
174
+ /** Longest gap between two inlets of a kerb, metres. */
175
+ readonly inletSpacing: number;
176
+ /** Each corner of a crossing has an inlet within this distance of the crossing's gallery box, metres. */
177
+ readonly cornerReach: number;
168
178
  };
169
179
  /** Least vertical gap between two pipes that cross, and which one goes above (lower rank above). */
170
180
  readonly crossing: {
@@ -314,6 +324,10 @@ declare const NETWORK_VIOLATION: {
314
324
  readonly CROSSING_ORDER: "crossing_order";
315
325
  readonly LATERAL_TOO_DEEP: "lateral_too_deep";
316
326
  readonly MANHOLE_SPACING: "manhole_spacing";
327
+ readonly PV_MISSING: "pv_missing";
328
+ readonly INLET_SPACING: "inlet_spacing";
329
+ readonly INLET_AT_CORNER: "inlet_at_corner";
330
+ readonly BRANCH_SKEWED: "branch_skewed";
317
331
  readonly UNKNOWN_TRANSFORMER: "unknown_transformer";
318
332
  readonly TRANSFORMER_OVERLOADED: "transformer_overloaded";
319
333
  readonly LV_CIRCUIT_TOO_LONG: "lv_circuit_too_long";
@@ -644,4 +658,213 @@ type PoleHeights = {
644
658
  };
645
659
  declare function poleHeightsOf(slots: PoleSlots): PoleHeights;
646
660
 
647
- export { type AerialClass, type AerialEdgeKind, type BuriedEdgeKind, CONNECTION_NETWORK, type ClearanceRow, type CoverRange, DEFAULT_NETWORK_LAYOUT, DEFAULT_NETWORK_RULES, DEFAULT_POLE_SLOTS, DISTRIBUTION, type DefaultConnectionRules, DistributionSpec, type DiversityBand, type DropMisfit, EDGE_CLASS, EDGE_NETWORK, type GenerateNetworksParams, type GeneratedNetworks, type GroundAt, type HardwareBox, type HardwareCylinder, type HardwareRole, type LotFrontage, NETWORK_ENDS, NETWORK_FLOW, NETWORK_VIOLATION, type NetworkBuilding, type NetworkClass, type NetworkCrown, type NetworkEdge, type NetworkEdgeKind, type NetworkGraph, type NetworkKind, type NetworkLamp, type NetworkLampsParams, type NetworkLayoutRules, type NetworkLot, type NetworkNode, type NetworkNodeKind, type NetworkRules, type NetworkSurface, type NetworkViolation, type NetworkViolationCode, type NetworkWarning, POLE_SLOT_GAPS, POLE_SLOT_ORDER, type PoleAttachments, type PoleHardware, type PoleHardwareOptions, type PoleHeights, type PolePoint, PoleSlotBand, type PoleSlotViolation, PoleSlots, type PowerRules, type RunEnd, type ServedLot, type StreetRun, type TransformerRating, type UtilityNetworks, type ValidateNetworksOptions, buildNetworkGraph, checkPoleSlots, circuitOf, connectedNodes, defaultConnectionsOf, distributionHardwareOf, diversifiedDemand, dropMisfitOf, edgeLength, frontagesOf, generateNetworks, isAerialEdgeKind, lotUnderDrop, networkLampsOf, pickTransformer, poleHeightsOf, resolveNetworkRules, slotsOfPole, surfaceOf, validateNetworks };
661
+ /** What shows of the buried networks: each one a piece standing at a node of the ground. */
662
+ type SurfaceKind = 'manhole_cover' | 'cleanout_cover' | 'inlet_grate' | 'valve_cover' | 'hydrant' | 'wing_wall' | 'inspection_cover' | 'meter_box' | 'downspout_mouth';
663
+ /** Ids of `game-destruction` materials. */
664
+ type SurfaceMaterial = 'cast_iron' | 'concrete' | 'plastic';
665
+ /** The pit under a cover, from the ground down to the pipe's bottom: round (a PV, a valve) or square (an inlet box, an inspection box). */
666
+ type Shaft = {
667
+ readonly shape: 'round' | 'square';
668
+ readonly width: number;
669
+ readonly depth: number;
670
+ readonly top: number;
671
+ readonly bottom: number;
672
+ readonly material: SurfaceMaterial;
673
+ };
674
+ type SurfacePiece = {
675
+ /** The id of the node it stands at. */
676
+ readonly id: string;
677
+ readonly kind: SurfaceKind;
678
+ /** Ground level at the node. */
679
+ readonly at: readonly [number, number, number];
680
+ /** Turn about y: the piece's local x runs along the street (the kerb), its local z (the wing wall's front) across it. */
681
+ readonly ry: number;
682
+ readonly spec: string | undefined;
683
+ readonly shape: 'round' | 'square';
684
+ /** Width along the street, height over the ground, depth across it (a round piece: its diameter twice). */
685
+ readonly size: readonly [number, number, number];
686
+ readonly material: SurfaceMaterial;
687
+ /** It blocks a body (a hydrant, a wing wall, a meter box). A cover and a grate are ground: they are walked on and carry a vehicle. */
688
+ readonly solid: boolean;
689
+ readonly shaft?: Shaft;
690
+ };
691
+ /** Where `drawRoad` lays the asphalt over the terrain (`game-urban` streets): a cover on the road stands on it, not on the ground under it. */
692
+ declare const ROAD_SURFACE_LEVEL = 0.02;
693
+ type SurfacePiecesOptions = {
694
+ /** What lies under a point (`GeneratedNetworks.surfaceAt`): a piece on the road stands `roadLevel` higher. Without it every piece stands on the ground. */
695
+ readonly surfaceAt?: (x: number, z: number) => NetworkSurface | undefined;
696
+ /** Default `ROAD_SURFACE_LEVEL`. */
697
+ readonly roadLevel?: number;
698
+ };
699
+ /**
700
+ * What shows of the buried networks, a piece at each node that has a face on the ground: the PV and gallery box covers, the cleanout and valve
701
+ * covers, the inlets' grates at the kerb, the hydrants, the wing walls at the outfalls, and at each lot the inspection box cover, the meter box
702
+ * and the downspout's mouth. Each carries its pit down to the pipe's bottom.
703
+ */
704
+ declare function surfacePiecesOf(networks: Pick<UtilityNetworks, 'nodes' | 'edges'>, options?: SurfacePiecesOptions): SurfacePiece[];
705
+
706
+ /** What leaks from a pipe that breaks. */
707
+ type PipeContent = 'sewage' | 'stormwater' | 'water';
708
+ /** A buried pipe as a body in the ground: its centre line from end to end, laid at the depths the network was laid with. */
709
+ type PipePiece = {
710
+ /** The id of the edge. */
711
+ readonly id: string;
712
+ readonly kind: BuriedEdgeKind;
713
+ readonly content: PipeContent;
714
+ /** The centre line's ends (world metres, `y` the centre of the bore). */
715
+ readonly from: readonly [number, number, number];
716
+ readonly to: readonly [number, number, number];
717
+ /** Outer diameter, metres. */
718
+ readonly diameter: number;
719
+ readonly material: SurfaceMaterial;
720
+ /** Ground level above each end. */
721
+ readonly ground: readonly [number, number];
722
+ };
723
+ /**
724
+ * Every pipe of the sewer, the drainage and the water as a piece laid in the ground: from the inverts of its ends (its own, or its nodes'),
725
+ * with the diameter the network gave plus its wall. The gutters are open channels at the kerb and have no pipe.
726
+ */
727
+ declare function pipePiecesOf(networks: Pick<UtilityNetworks, 'nodes' | 'edges'>): PipePiece[];
728
+ /** Metres of ground over the top of a pipe at a share `t` of its length. */
729
+ declare function coverOf(pipe: PipePiece, t: number): number;
730
+
731
+ type PipeBreach = {
732
+ readonly id: string;
733
+ readonly kind: PipePiece['kind'];
734
+ readonly content: PipeContent;
735
+ /** Where along the pipe (0 at its `from` end) the bowl reaches deepest, and the point of the centre line there. */
736
+ readonly share: number;
737
+ readonly at: readonly [number, number, number];
738
+ /** The bowl got down to the bore's centre line: the pipe is broken. */
739
+ readonly severed: boolean;
740
+ };
741
+ /** A bowl taken out of the ground: the shape of a `GroundLoss` (`game-contracts`) or of a terrain blow's result. */
742
+ type Crater = {
743
+ readonly x: number;
744
+ readonly z: number;
745
+ readonly radius: number;
746
+ readonly depth: number;
747
+ };
748
+ /** How deep the bowl is at a distance from its centre: a paraboloid, the full depth at the middle and nothing at the rim. */
749
+ declare function bowlDepthAt(crater: Crater, distance: number): number;
750
+ type Breach = {
751
+ /** Pipes the bowl reached (`severed`: down to the centre line, so the pipe is broken and leaks). */
752
+ readonly pipes: readonly PipeBreach[];
753
+ /** The covers, grates and boxes inside the bowl: they are thrown off, and the pit under them lies open. */
754
+ readonly surface: readonly string[];
755
+ /** What leaks, once, of the pipes severed. */
756
+ readonly leaks: readonly PipeContent[];
757
+ };
758
+ /**
759
+ * What a crater does to the networks under it: the pipes whose top the bowl got down to, and the surface pieces it swallowed. Pure, from
760
+ * the pieces `pipePiecesOf` and `surfacePiecesOf` give, so a host calls it with the result of a terrain blow (`world.ground.on('blow')`)
761
+ * and the same answer comes for the same bowl.
762
+ */
763
+ declare function breachOf({ pipes, surface }: {
764
+ readonly pipes: readonly PipePiece[];
765
+ readonly surface: readonly SurfacePiece[];
766
+ }, crater: Crater): Breach;
767
+
768
+ /** Id of the one part of a cover's plate in its `DestructibleSpec`: the drawing binds its meshes to it. */
769
+ declare const COVER_PLATE_PART = "plate";
770
+ /**
771
+ * The hole a cover leaves where it was thrown off: the square of the shaft under it, down to the shaft's floor, as the ground dug away
772
+ * (`GroundExcavation`, what `Terrain.open` takes). Undefined for a piece with no pit under it, or one that is no cover (a hydrant, a wing wall).
773
+ */
774
+ declare function openingOf(piece: SurfacePiece): GroundExcavation | undefined;
775
+ /** A cover as a loose piece: a plate of its material (cast iron) in the shape of its frame, as the destruction world takes it, and where it stood. */
776
+ type CoverBody = {
777
+ readonly id: string;
778
+ readonly spec: DestructibleSpec;
779
+ readonly position: readonly [number, number, number];
780
+ /** Turn about y, radians. */
781
+ readonly ry: number;
782
+ /** Kilograms the plate weighs: density times volume (`game-destruction` gives the same when the part has no mass). */
783
+ readonly mass: number;
784
+ };
785
+ declare function coverBodyOf(piece: SurfacePiece): CoverBody | undefined;
786
+ /**
787
+ * The velocity a crater throws a cover off with: away from the middle of the bowl (along +x for a cover right at it), faster the deeper the bowl, and up. Pure: the same bowl
788
+ * throws the same cover the same way, so a LAN client that repeats the blow sees the same lid land in the same place.
789
+ */
790
+ declare function coverThrowOf(piece: SurfacePiece, crater: {
791
+ readonly x: number;
792
+ readonly z: number;
793
+ readonly depth: number;
794
+ }): readonly [number, number, number];
795
+
796
+ /** What one broken pipe lets out: the fluid, where, how fast at most, and how much is there to lose (finite: a valve closes, a pipe empties). */
797
+ type NetworkLeak = {
798
+ /** The id of the pipe (its edge). */
799
+ readonly id: string;
800
+ readonly content: PipeContent;
801
+ /** The break: world metres, `y` the pipe's centre line. */
802
+ readonly at: readonly [number, number, number];
803
+ readonly diameter: number;
804
+ /** Litres per second at most. */
805
+ readonly rate: number;
806
+ /** Litres there are to lose, in all. */
807
+ readonly volume: number;
808
+ /** It leaves under pressure (a water main), up out of the ground; the rest runs. */
809
+ readonly isPressurised: boolean;
810
+ };
811
+ /** The houses that lose their water, and the nodes of the network (hydrants, valves, services) that go with them. */
812
+ type NetworkOutage = {
813
+ readonly network: 'water';
814
+ /** The pipes whose break cut the supply. */
815
+ readonly causes: readonly string[];
816
+ /** Nodes the reservoir no longer reaches. */
817
+ readonly nodes: readonly string[];
818
+ /** Lots (`NetworkNode.lot`) with no water. */
819
+ readonly lots: readonly string[];
820
+ };
821
+ /** What a blast on the networks tells the host, in the order it happens: a pipe leaks, a house loses its water, a cover opens its hole. */
822
+ type NetworkEvent = {
823
+ readonly type: 'network_leak';
824
+ readonly leak: NetworkLeak;
825
+ } | {
826
+ readonly type: 'network_outage';
827
+ readonly outage: NetworkOutage;
828
+ } | {
829
+ readonly type: 'network_hole';
830
+ readonly id: string;
831
+ readonly kind: string;
832
+ };
833
+ type LeakRule = {
834
+ /** Litres per second of the break (a bored main, a service). */
835
+ readonly mainRate: number;
836
+ readonly branchRate: number;
837
+ /** Seconds before the utility closes the valve (water), the inflow stops (sewage) or the rain does (drainage): the volume is `rate` times this. */
838
+ readonly seconds: number;
839
+ readonly isPressurised: boolean;
840
+ };
841
+ type LeakRules = Readonly<Record<PipeContent, LeakRule>>;
842
+ declare const DEFAULT_LEAK_RULES: LeakRules;
843
+ type LeaksOptions = {
844
+ /** The drainage only runs while it rains (`game-weather`: `effects.rain` above zero). */
845
+ readonly isRaining: boolean;
846
+ readonly rules?: LeakRules;
847
+ };
848
+ /**
849
+ * The leaks a breach opens: one for each pipe the bowl severed, of its content, at the break. Water runs under pressure and comes up out of the crater; sewage runs slow; stormwater only runs
850
+ * when it rains. Each is finite (`rate` for `seconds`): `drainLeak` gives what a step lets out until nothing is left. Pure, in the order of the breach.
851
+ */
852
+ declare function leaksOf(networks: Pick<UtilityNetworks, 'nodes' | 'edges'>, breach: Breach, { isRaining, rules }: LeaksOptions): NetworkLeak[];
853
+ /** A leak being let out: what is left of it. */
854
+ type LeakState = {
855
+ readonly leak: NetworkLeak;
856
+ remaining: number;
857
+ };
858
+ declare function startLeak(leak: NetworkLeak): LeakState;
859
+ /** Litres a step of `dt` seconds lets out of a leak (never more than its rate, never more than is left); takes them from `remaining`. */
860
+ declare function drainLeak(state: LeakState, dt: number): number;
861
+ /**
862
+ * The houses that lose their water when the pipes in `after` are broken and the ones in `before` already were: the nodes the reservoir reached then and reaches no more,
863
+ * and the lots among them. Pure, from the graph walked once without the severed edges (a tree loses everything downstream of the break).
864
+ */
865
+ declare function waterOutageOf(networks: Pick<UtilityNetworks, 'nodes' | 'edges'>, { before, after }: {
866
+ readonly before: readonly string[];
867
+ readonly after: readonly string[];
868
+ }): NetworkOutage | undefined;
869
+
870
+ export { type AerialClass, type AerialEdgeKind, type Breach, type BuriedEdgeKind, CONNECTION_NETWORK, COVER_PLATE_PART, type ClearanceRow, type CoverBody, type CoverRange, type Crater, DEFAULT_LEAK_RULES, DEFAULT_NETWORK_LAYOUT, DEFAULT_NETWORK_RULES, DEFAULT_POLE_SLOTS, DISTRIBUTION, type DefaultConnectionRules, DistributionSpec, type DiversityBand, type DropMisfit, EDGE_CLASS, EDGE_NETWORK, type GenerateNetworksParams, type GeneratedNetworks, type GroundAt, type HardwareBox, type HardwareCylinder, type HardwareRole, type LeakRule, type LeakRules, type LeakState, type LeaksOptions, type LotFrontage, NETWORK_ENDS, NETWORK_FLOW, NETWORK_VIOLATION, type NetworkBuilding, type NetworkClass, type NetworkCrown, type NetworkEdge, type NetworkEdgeKind, type NetworkEvent, type NetworkGraph, type NetworkKind, type NetworkLamp, type NetworkLampsParams, type NetworkLayoutRules, type NetworkLeak, type NetworkLot, type NetworkNode, type NetworkNodeKind, type NetworkOutage, type NetworkRules, type NetworkSurface, type NetworkViolation, type NetworkViolationCode, type NetworkWarning, POLE_SLOT_GAPS, POLE_SLOT_ORDER, type PipeBreach, type PipeContent, type PipePiece, type PoleAttachments, type PoleHardware, type PoleHardwareOptions, type PoleHeights, type PolePoint, PoleSlotBand, type PoleSlotViolation, PoleSlots, type PowerRules, ROAD_SURFACE_LEVEL, type RunEnd, type ServedLot, type Shaft, type StreetRun, type SurfaceKind, type SurfaceMaterial, type SurfacePiece, type SurfacePiecesOptions, type TransformerRating, type UtilityNetworks, type ValidateNetworksOptions, bowlDepthAt, breachOf, buildNetworkGraph, checkPoleSlots, circuitOf, connectedNodes, coverBodyOf, coverOf, coverThrowOf, defaultConnectionsOf, distributionHardwareOf, diversifiedDemand, drainLeak, dropMisfitOf, edgeLength, frontagesOf, generateNetworks, isAerialEdgeKind, leaksOf, lotUnderDrop, networkLampsOf, openingOf, pickTransformer, pipePiecesOf, poleHeightsOf, resolveNetworkRules, slotsOfPole, startLeak, surfaceOf, surfacePiecesOf, validateNetworks, waterOutageOf };
@@ -93,6 +93,10 @@ var NETWORK_VIOLATION = {
93
93
  CROSSING_ORDER: "crossing_order",
94
94
  LATERAL_TOO_DEEP: "lateral_too_deep",
95
95
  MANHOLE_SPACING: "manhole_spacing",
96
+ PV_MISSING: "pv_missing",
97
+ INLET_SPACING: "inlet_spacing",
98
+ INLET_AT_CORNER: "inlet_at_corner",
99
+ BRANCH_SKEWED: "branch_skewed",
96
100
  UNKNOWN_TRANSFORMER: "unknown_transformer",
97
101
  TRANSFORMER_OVERLOADED: "transformer_overloaded",
98
102
  LV_CIRCUIT_TOO_LONG: "lv_circuit_too_long"
@@ -232,7 +236,8 @@ var DEFAULT_NETWORK_RULES = {
232
236
  storm_gutter: { min: 0 },
233
237
  sewer_main: { min: 0.9 }
234
238
  },
235
- sewer: { maxDepth: 4.5, coverUnderGallery: 1.8, galleryReach: 3, lateralOverGallery: 0.85, manholeSpacing: 80 },
239
+ sewer: { maxDepth: 4.5, coverUnderGallery: 1.8, galleryReach: 3, lateralOverGallery: 0.85, manholeSpacing: 80, turnTolerance: 5, branchMaxSkew: 5 },
240
+ storm: { inletSpacing: 60, cornerReach: 15 },
236
241
  crossing: { separation: 0.2, rank: { storm_gutter: 0, water_main: 1, water_service: 1, sewer_lateral: 2, storm_lateral: 3, storm_gallery: 3, sewer_main: 4 } },
237
242
  connectionTolerance: 0.01,
238
243
  layout: DEFAULT_NETWORK_LAYOUT
@@ -667,6 +672,91 @@ function validateLoad(context) {
667
672
  }
668
673
  }
669
674
 
675
+ // src/networks/validate-surface.ts
676
+ var MAIN_KINDS = /* @__PURE__ */ new Set(["sewer_main", "storm_gallery"]);
677
+ var PIT_KINDS = /* @__PURE__ */ new Set(["manhole", "pumping_station", "treatment", "outfall"]);
678
+ var DEGREES = Math.PI / 180;
679
+ function unit(dx, dz) {
680
+ const length2 = Math.hypot(dx, dz);
681
+ return length2 === 0 ? [0, 0] : [dx / length2, dz / length2];
682
+ }
683
+ function mainsAt(graph, node) {
684
+ const leaving = graph.outgoing(node.id).filter((edge) => MAIN_KINDS.has(edge.kind)).map((edge) => ({ edge, other: graph.nodes.get(edge.to) }));
685
+ const reaching = graph.incoming(node.id).filter((edge) => MAIN_KINDS.has(edge.kind)).map((edge) => ({ edge, other: graph.nodes.get(edge.from) }));
686
+ return [...leaving, ...reaching].flatMap(({ edge, other }) => other === void 0 || other.at[0] === node.at[0] && other.at[2] === node.at[2] ? [] : [{ edge, away: unit(other.at[0] - node.at[0], other.at[2] - node.at[2]) }]);
687
+ }
688
+ function validatePits(context) {
689
+ const tolerance = Math.cos((180 - context.rules.sewer.turnTolerance) * DEGREES);
690
+ for (const node of context.graph.nodes.values()) {
691
+ if (PIT_KINDS.has(node.kind)) continue;
692
+ const mains = mainsAt(context.graph, node);
693
+ const [first, second] = mains;
694
+ const meets = mains.length >= 3;
695
+ const turns = mains.length === 2 && first !== void 0 && second !== void 0 && first.away[0] * second.away[0] + first.away[1] * second.away[1] > tolerance;
696
+ if (meets || turns) context.report({ code: NETWORK_VIOLATION.PV_MISSING, id: node.id, detail: meets ? "crossing" : "turn" });
697
+ }
698
+ }
699
+ var kerbOf = (id) => id.slice(0, id.lastIndexOf(":"));
700
+ function validateInletSpacing(context, inlets) {
701
+ const kerbs = /* @__PURE__ */ new Map();
702
+ for (const inlet of inlets) kerbs.set(kerbOf(inlet.id), [...kerbs.get(kerbOf(inlet.id)) ?? [], inlet]);
703
+ const limit = context.rules.storm.inletSpacing;
704
+ for (const kerb of kerbs.values()) {
705
+ const alongX = Math.max(...kerb.map((inlet) => inlet.at[0])) - Math.min(...kerb.map((inlet) => inlet.at[0])) >= Math.max(...kerb.map((inlet) => inlet.at[2])) - Math.min(...kerb.map((inlet) => inlet.at[2]));
706
+ const ordered = [...kerb].sort((a, b) => alongX ? a.at[0] - b.at[0] : a.at[2] - b.at[2]);
707
+ ordered.slice(1).forEach((inlet, index) => {
708
+ const previous = ordered[index];
709
+ const gap = Math.hypot(inlet.at[0] - previous.at[0], inlet.at[2] - previous.at[2]);
710
+ if (gap > limit + 1e-6) context.report({ code: NETWORK_VIOLATION.INLET_SPACING, id: inlet.id, value: gap, limit });
711
+ });
712
+ }
713
+ }
714
+ function cornersOf(mains) {
715
+ const signs = (axis) => [...new Set(mains.filter((main) => Math.abs(main.away[axis]) > 0.7).map((main) => Math.sign(main.away[axis])))];
716
+ return signs(0).flatMap((x) => signs(1).map((z) => [x, z]));
717
+ }
718
+ function validateInletCorners(context, inlets) {
719
+ const reach = context.rules.storm.cornerReach;
720
+ for (const node of context.graph.nodes.values()) {
721
+ if (node.kind !== "manhole" || !node.carries.includes("storm")) continue;
722
+ const mains = mainsAt(context.graph, node);
723
+ if (mains.length < 3) continue;
724
+ for (const [x, z] of cornersOf(mains)) {
725
+ const found = inlets.some((inlet) => Math.sign(inlet.at[0] - node.at[0]) === x && Math.sign(inlet.at[2] - node.at[2]) === z && Math.hypot(inlet.at[0] - node.at[0], inlet.at[2] - node.at[2]) <= reach);
726
+ if (!found) context.report({ code: NETWORK_VIOLATION.INLET_AT_CORNER, id: node.id, detail: `${x > 0 ? "+" : "-"}x${z > 0 ? "+" : "-"}z`, limit: reach });
727
+ }
728
+ }
729
+ }
730
+ function streetAt(graph, nodeId, seen = /* @__PURE__ */ new Set()) {
731
+ const node = graph.nodes.get(nodeId);
732
+ if (node === void 0 || seen.has(nodeId)) return void 0;
733
+ const [main] = mainsAt(graph, node);
734
+ if (main !== void 0) return main.away;
735
+ const onward = graph.outgoing(nodeId).find((edge) => edge.kind === "sewer_lateral" || edge.kind === "storm_lateral");
736
+ return onward === void 0 ? void 0 : streetAt(graph, onward.to, /* @__PURE__ */ new Set([...seen, nodeId]));
737
+ }
738
+ function validateBranches(context) {
739
+ const limit = context.rules.sewer.branchMaxSkew;
740
+ for (const edge of context.graph.edges.values()) {
741
+ if (edge.kind !== "sewer_lateral" && edge.kind !== "storm_lateral") continue;
742
+ const from = context.graph.nodes.get(edge.from);
743
+ const to = context.graph.nodes.get(edge.to);
744
+ const street = streetAt(context.graph, edge.to);
745
+ if (from === void 0 || to === void 0 || street === void 0) continue;
746
+ const branch = unit(to.at[0] - from.at[0], to.at[2] - from.at[2]);
747
+ if (branch[0] === 0 && branch[1] === 0) continue;
748
+ const skew = Math.abs(90 - Math.acos(Math.min(1, Math.abs(branch[0] * street[0] + branch[1] * street[1]))) / DEGREES);
749
+ if (skew > limit) context.report({ code: NETWORK_VIOLATION.BRANCH_SKEWED, id: edge.id, value: skew, limit });
750
+ }
751
+ }
752
+ function validateSurface(context) {
753
+ const inlets = [...context.graph.nodes.values()].filter((node) => node.kind === "inlet");
754
+ validatePits(context);
755
+ validateInletSpacing(context, inlets);
756
+ validateInletCorners(context, inlets);
757
+ validateBranches(context);
758
+ }
759
+
670
760
  // src/networks/validate-structure.ts
671
761
  var NETWORKS = ["power_mv", "power_lv", "lighting", "telecom", "sewer", "storm", "water"];
672
762
  function validateIds(context, networks) {
@@ -749,6 +839,7 @@ function validateNetworks(networks, options = {}) {
749
839
  validateAerial(context);
750
840
  validateDrops(context);
751
841
  validateBuried(context);
842
+ validateSurface(context);
752
843
  validateLoad(context);
753
844
  return violations;
754
845
  }
@@ -2033,8 +2124,217 @@ function distributionHardwareOf(spec, o) {
2033
2124
  const attachments = { mv: mv.mv, lv: lvAt, telecom: telecomAt, guy: [0, spec.slots.bands.mv[0] - 0.15, 0], ...transformer ? { transformer: transformer.points } : {} };
2034
2125
  return { boxes, cylinders, ...lamp ? { lens: lamp.lens } : {}, attachments };
2035
2126
  }
2127
+
2128
+ // src/networks/surface.ts
2129
+ var COVER = 0.6;
2130
+ var SHAFT_PV = { shape: "round", width: 0.9, depth: 0.9, material: "concrete" };
2131
+ function specOf(node) {
2132
+ const network = node.carries[0];
2133
+ switch (node.kind) {
2134
+ case "manhole":
2135
+ return node.spec === "gallery_box" ? { kind: "manhole_cover", shape: "square", size: [COVER, 0.03, COVER], material: "cast_iron", solid: false, shaft: { shape: "square", width: 0.9, depth: 0.9, material: "concrete" } } : { kind: "manhole_cover", shape: "round", size: [COVER, 0.03, COVER], material: "cast_iron", solid: false, shaft: SHAFT_PV };
2136
+ case "pumping_station":
2137
+ return { kind: "manhole_cover", shape: "round", size: [0.9, 0.03, 0.9], material: "cast_iron", solid: false, shaft: { shape: "round", width: 1.5, depth: 1.5, material: "concrete" } };
2138
+ case "cleanout":
2139
+ return { kind: "cleanout_cover", shape: "round", size: [0.25, 0.03, 0.25], material: "cast_iron", solid: false, shaft: { shape: "round", width: 0.2, depth: 0.2, material: "plastic" } };
2140
+ case "inlet":
2141
+ return { kind: "inlet_grate", shape: "square", size: [0.9, 0.03, 0.5], material: "cast_iron", solid: false, shaft: { shape: "square", width: 0.8, depth: 0.5, material: "concrete" } };
2142
+ case "valve":
2143
+ return { kind: "valve_cover", shape: "round", size: [0.2, 0.03, 0.2], material: "cast_iron", solid: false, shaft: { shape: "round", width: 0.1, depth: 0.1, material: "cast_iron" } };
2144
+ case "hydrant":
2145
+ return { kind: "hydrant", shape: "round", size: [0.2, 0.8, 0.2], material: "cast_iron", solid: true, shaft: { shape: "round", width: 0.1, depth: 0.1, material: "cast_iron" } };
2146
+ case "outfall":
2147
+ return { kind: "wing_wall", shape: "square", size: [3, 0.8, 1.6], material: "concrete", solid: true };
2148
+ case "connection":
2149
+ if (network === "sewer") return { kind: "inspection_cover", shape: "square", size: [0.6, 0.03, 0.6], material: "concrete", solid: false, shaft: { shape: "square", width: 0.6, depth: 0.6, material: "concrete" } };
2150
+ if (network === "water") return { kind: "meter_box", shape: "square", size: [0.4, 0.3, 0.25], material: "concrete", solid: true, shaft: { shape: "round", width: 0.04, depth: 0.04, material: "plastic" } };
2151
+ return network === "storm" ? { kind: "downspout_mouth", shape: "square", size: [0.3, 0.02, 0.2], material: "concrete", solid: false } : void 0;
2152
+ default:
2153
+ return void 0;
2154
+ }
2155
+ }
2156
+ var unit2 = (dx, dz) => {
2157
+ const length2 = Math.hypot(dx, dz);
2158
+ return length2 === 0 ? [1, 0] : [dx / length2, dz / length2];
2159
+ };
2160
+ var planOf2 = (graph, id, from) => {
2161
+ const other = graph.nodes.get(id);
2162
+ return other === void 0 ? [0, 0] : [other.at[0] - from.at[0], other.at[2] - from.at[2]];
2163
+ };
2164
+ function alongOf(graph, node) {
2165
+ const edges = [...graph.outgoing(node.id), ...graph.incoming(node.id)];
2166
+ const square = edges.find((edge) => edge.kind === "storm_lateral" || edge.kind === "sewer_lateral" || edge.kind === "water_service");
2167
+ if (square !== void 0) {
2168
+ const [dx2, dz2] = planOf2(graph, square.from === node.id ? square.to : square.from, node);
2169
+ return unit2(-dz2, dx2);
2170
+ }
2171
+ const along = edges.find((edge) => edge.kind === "storm_gutter" || edge.kind === "storm_gallery" || edge.kind === "sewer_main");
2172
+ if (along === void 0) return [1, 0];
2173
+ const [dx, dz] = planOf2(graph, along.from === node.id ? along.to : along.from, node);
2174
+ return unit2(dx, dz);
2175
+ }
2176
+ function headingOf(graph, node) {
2177
+ if (node.kind === "outfall") {
2178
+ const into = graph.incoming(node.id).find((edge) => edge.kind === "storm_gallery");
2179
+ const [bx, bz] = into === void 0 ? [0, -1] : planOf2(graph, into.from, node);
2180
+ const [fx, fz] = unit2(-bx, -bz);
2181
+ return Math.atan2(fx, fz);
2182
+ }
2183
+ const [dx, dz] = alongOf(graph, node);
2184
+ return Math.atan2(-dz, dx);
2185
+ }
2186
+ var ROAD_SURFACE_LEVEL = 0.02;
2187
+ function surfacePiecesOf(networks, options = {}) {
2188
+ const graph = buildNetworkGraph(networks);
2189
+ const lift = (node) => options.surfaceAt?.(node.at[0], node.at[2]) === "road" ? options.roadLevel ?? ROAD_SURFACE_LEVEL : 0;
2190
+ const taken = [];
2191
+ return networks.nodes.flatMap((node) => {
2192
+ const spec = specOf(node);
2193
+ if (spec === void 0) return [];
2194
+ const reach = spec.size[0] + 0.12;
2195
+ if (taken.some((other) => other.kind === spec.kind && Math.hypot(other.x - node.at[0], other.z - node.at[2]) < reach)) return [];
2196
+ taken.push({ kind: spec.kind, x: node.at[0], z: node.at[2] });
2197
+ const { shaft, ...piece } = spec;
2198
+ const bottom = node.invert;
2199
+ return [{ id: node.id, ...piece, at: [node.at[0], node.at[1] + lift(node), node.at[2]], ry: headingOf(graph, node), spec: node.spec, ...shaft === void 0 || bottom === void 0 ? {} : { shaft: { ...shaft, top: node.at[1] + lift(node), bottom } } }];
2200
+ });
2201
+ }
2202
+
2203
+ // src/networks/pipes.ts
2204
+ var CONTENT = {
2205
+ sewer_main: "sewage",
2206
+ sewer_lateral: "sewage",
2207
+ storm_gallery: "stormwater",
2208
+ storm_lateral: "stormwater",
2209
+ water_main: "water",
2210
+ water_service: "water"
2211
+ };
2212
+ var MATERIAL_OF = { sewage: "plastic", water: "plastic", stormwater: "concrete" };
2213
+ var WALL_SHARE = 0.12;
2214
+ var HYDRANT_BRANCH_SHARE = 0.8;
2215
+ function pipePiecesOf(networks) {
2216
+ const nodes = new Map(networks.nodes.map((node) => [node.id, node]));
2217
+ return networks.edges.flatMap((edge) => {
2218
+ const content = isAerialEdgeKind(edge.kind) ? void 0 : CONTENT[edge.kind];
2219
+ const [from, to] = [nodes.get(edge.from), nodes.get(edge.to)];
2220
+ if (content === void 0 || from === void 0 || to === void 0 || edge.diameter === void 0) return [];
2221
+ const inverts = edge.inverts ?? (from.invert !== void 0 && to.invert !== void 0 ? [from.invert, to.invert] : void 0);
2222
+ if (inverts === void 0) return [];
2223
+ const diameter = edge.diameter * (1 + WALL_SHARE) * (to.kind === "hydrant" ? HYDRANT_BRANCH_SHARE : 1);
2224
+ const centre = (invert) => invert + edge.diameter / 2;
2225
+ return [{ id: edge.id, kind: edge.kind, content, from: [from.at[0], centre(inverts[0]), from.at[2]], to: [to.at[0], centre(inverts[1]), to.at[2]], diameter, material: MATERIAL_OF[content], ground: [from.at[1], to.at[1]] }];
2226
+ });
2227
+ }
2228
+ function coverOf(pipe, t) {
2229
+ const ground = pipe.ground[0] + (pipe.ground[1] - pipe.ground[0]) * t;
2230
+ const centre = pipe.from[1] + (pipe.to[1] - pipe.from[1]) * t;
2231
+ return ground - (centre + pipe.diameter / 2);
2232
+ }
2233
+
2234
+ // src/networks/breach.ts
2235
+ var SAMPLE_STEP = 0.25;
2236
+ function bowlDepthAt(crater, distance2) {
2237
+ if (distance2 >= crater.radius) return 0;
2238
+ return crater.depth * (1 - (distance2 / crater.radius) ** 2);
2239
+ }
2240
+ function reachOf2(pipe, crater) {
2241
+ const length2 = Math.hypot(pipe.to[0] - pipe.from[0], pipe.to[2] - pipe.from[2]);
2242
+ const steps = Math.max(1, Math.ceil(length2 / SAMPLE_STEP));
2243
+ let best;
2244
+ for (let step = 0; step <= steps; step++) {
2245
+ const share2 = step / steps;
2246
+ const x = pipe.from[0] + (pipe.to[0] - pipe.from[0]) * share2;
2247
+ const z = pipe.from[2] + (pipe.to[2] - pipe.from[2]) * share2;
2248
+ const margin = bowlDepthAt(crater, Math.hypot(x - crater.x, z - crater.z)) - coverOf(pipe, share2);
2249
+ if (margin >= 0 && (best === void 0 || margin > best.margin)) best = { share: share2, margin };
2250
+ }
2251
+ if (best === void 0) return void 0;
2252
+ const { share } = best;
2253
+ const lerp2 = (a, b) => a + (b - a) * share;
2254
+ return { id: pipe.id, kind: pipe.kind, content: pipe.content, share, at: [lerp2(pipe.from[0], pipe.to[0]), lerp2(pipe.from[1], pipe.to[1]), lerp2(pipe.from[2], pipe.to[2])], severed: best.margin >= pipe.diameter / 2 };
2255
+ }
2256
+ function breachOf({ pipes, surface }, crater) {
2257
+ const reached = pipes.flatMap((pipe) => reachOf2(pipe, crater) ?? []);
2258
+ const covers = surface.filter((piece) => Math.hypot(piece.at[0] - crater.x, piece.at[2] - crater.z) <= crater.radius && bowlDepthAt(crater, Math.hypot(piece.at[0] - crater.x, piece.at[2] - crater.z)) > piece.size[1]).map((piece) => piece.id);
2259
+ return { pipes: reached, surface: covers, leaks: [...new Set(reached.filter((pipe) => pipe.severed).map((pipe) => pipe.content))] };
2260
+ }
2261
+
2262
+ // src/networks/cover.ts
2263
+ var COVER_PLATE_PART = "plate";
2264
+ var PIT_FLOOR = 0.1;
2265
+ function openingOf(piece) {
2266
+ if (piece.shaft === void 0 || piece.solid) return void 0;
2267
+ const half = piece.shaft.width / 2;
2268
+ return { rect: [piece.at[0] - half, piece.at[2] - half, piece.at[0] + half, piece.at[2] + half], depth: piece.at[1] - piece.shaft.bottom + PIT_FLOOR };
2269
+ }
2270
+ var DENSITY = { cast_iron: 7200, concrete: 2400, plastic: 950 };
2271
+ function coverBodyOf(piece) {
2272
+ if (piece.shaft === void 0 || piece.solid) return void 0;
2273
+ const [width, height, depth] = piece.size;
2274
+ const isRound = piece.shape === "round";
2275
+ const shape = isRound ? { kind: "cylinder", radius: width / 2, height } : { kind: "box", size: [width, height, depth] };
2276
+ const volume = isRound ? Math.PI * (width / 2) ** 2 * height : width * height * depth;
2277
+ return {
2278
+ id: `cover:${piece.id}`,
2279
+ spec: { name: piece.kind, tags: ["network_cover"], parts: [{ id: COVER_PLATE_PART, material: piece.material, shape, position: [0, height / 2, 0] }] },
2280
+ position: piece.at,
2281
+ ry: piece.ry,
2282
+ mass: volume * DENSITY[piece.material]
2283
+ };
2284
+ }
2285
+ var THROW_BASE = 1.5;
2286
+ var THROW_PER_DEPTH = 1.2;
2287
+ var THROW_UP = 2;
2288
+ function coverThrowOf(piece, crater) {
2289
+ const [dx, dz] = [piece.at[0] - crater.x, piece.at[2] - crater.z];
2290
+ const length2 = Math.hypot(dx, dz);
2291
+ const [ux, uz] = length2 < 1e-6 ? [1, 0] : [dx / length2, dz / length2];
2292
+ const out = THROW_BASE + THROW_PER_DEPTH * crater.depth;
2293
+ return [ux * out, THROW_UP * out, uz * out];
2294
+ }
2295
+
2296
+ // src/networks/leaks.ts
2297
+ var DEFAULT_LEAK_RULES = {
2298
+ water: { mainRate: 6, branchRate: 0.8, seconds: 300, isPressurised: true },
2299
+ sewage: { mainRate: 0.5, branchRate: 0.15, seconds: 600, isPressurised: false },
2300
+ stormwater: { mainRate: 2, branchRate: 0.6, seconds: 240, isPressurised: false }
2301
+ };
2302
+ var BRANCH_KINDS = /* @__PURE__ */ new Set(["water_service", "sewer_lateral", "storm_lateral"]);
2303
+ function leaksOf(networks, breach, { isRaining, rules = DEFAULT_LEAK_RULES }) {
2304
+ const pipes = new Map(pipePiecesOf(networks).map((pipe) => [pipe.id, pipe]));
2305
+ return breach.pipes.flatMap((reached) => {
2306
+ const pipe = pipes.get(reached.id);
2307
+ if (!reached.severed || pipe === void 0) return [];
2308
+ if (reached.content === "stormwater" && !isRaining) return [];
2309
+ const rule = rules[reached.content];
2310
+ const rate = BRANCH_KINDS.has(pipe.kind) ? rule.branchRate : rule.mainRate;
2311
+ return [{ id: reached.id, content: reached.content, at: reached.at, diameter: pipe.diameter, rate, volume: rate * rule.seconds, isPressurised: rule.isPressurised }];
2312
+ });
2313
+ }
2314
+ function startLeak(leak) {
2315
+ return { leak, remaining: leak.volume };
2316
+ }
2317
+ function drainLeak(state, dt) {
2318
+ const out = Math.min(state.leak.rate * Math.max(0, dt), state.remaining);
2319
+ state.remaining -= out;
2320
+ return out;
2321
+ }
2322
+ function waterOutageOf(networks, { before, after }) {
2323
+ const supplied = (severed) => {
2324
+ const gone = new Set(severed);
2325
+ return connectedNodes(buildNetworkGraph({ nodes: networks.nodes, edges: networks.edges.filter((edge) => !gone.has(edge.id)) }), "water");
2326
+ };
2327
+ const [was, is] = [supplied(before), supplied(after)];
2328
+ const lost = networks.nodes.filter((node) => node.carries.includes("water") && was.has(node.id) && !is.has(node.id));
2329
+ if (lost.length === 0) return void 0;
2330
+ const lots = [...new Set(lost.flatMap((node) => node.lot ?? []))];
2331
+ const already = new Set(before);
2332
+ return { network: "water", causes: after.filter((id) => !already.has(id)), nodes: lost.map((node) => node.id), lots };
2333
+ }
2036
2334
  export {
2037
2335
  CONNECTION_NETWORK,
2336
+ COVER_PLATE_PART,
2337
+ DEFAULT_LEAK_RULES,
2038
2338
  DEFAULT_NETWORK_LAYOUT,
2039
2339
  DEFAULT_NETWORK_RULES,
2040
2340
  DEFAULT_POLE_SLOTS,
@@ -2046,25 +2346,38 @@ export {
2046
2346
  NETWORK_VIOLATION,
2047
2347
  POLE_SLOT_GAPS,
2048
2348
  POLE_SLOT_ORDER,
2349
+ ROAD_SURFACE_LEVEL,
2350
+ bowlDepthAt,
2351
+ breachOf,
2049
2352
  buildNetworkGraph,
2050
2353
  checkPoleSlots,
2051
2354
  circuitOf,
2052
2355
  connectedNodes,
2356
+ coverBodyOf,
2357
+ coverOf,
2358
+ coverThrowOf,
2053
2359
  defaultConnectionsOf,
2054
2360
  distributionHardwareOf,
2055
2361
  diversifiedDemand,
2362
+ drainLeak,
2056
2363
  dropMisfitOf,
2057
2364
  edgeLength,
2058
2365
  frontagesOf,
2059
2366
  generateNetworks,
2060
2367
  isAerialEdgeKind,
2368
+ leaksOf,
2061
2369
  lotUnderDrop,
2062
2370
  networkLampsOf,
2371
+ openingOf,
2063
2372
  pickTransformer,
2373
+ pipePiecesOf,
2064
2374
  poleHeightsOf,
2065
2375
  resolveNetworkRules,
2066
2376
  slotsOfPole,
2377
+ startLeak,
2067
2378
  surfaceOf,
2068
- validateNetworks
2379
+ surfacePiecesOf,
2380
+ validateNetworks,
2381
+ waterOutageOf
2069
2382
  };
2070
2383
  //# sourceMappingURL=index.js.map