Scene sound graph (.SGP)¶
Every scene archive (SCENES/**.ZIP) holds one <scene>.SGP. It is the sound graph, not an AI path graph: ZEngineDataBase::GetSoundGraphSize and GetSoundGraphData (enginedatabase.cpp:510 and :516) load it under that name. It describes the portals ("exits") between rooms and every route of up to four portals between two rooms. The engine uses it for three things:
- which sounds the listener hears, and through which portals (occlusion), in
ZSoundGraph::GetAudiable; - which sound events NPCs hear (gunshots, voices, noises), through
CEVENTCONTROLLER::NotifyActors, which callsZSoundGraph::TestRooms; - door openness: doors write
SExit::m_fOpennessat run time.
The AI's path graph is the Pathfinder4 block inside the .GMS (see "AI data a level needs" below).
This file was read from src/engine/audio/soundengine/soundgraph.cpp and its callers. It was checked against all 71 SGPs on the disc. tools/port/scene/sgp.py parses and rewrites every one of them byte for byte (71/71 identical; also listed in roundtrip.py). The same module rebuilds a graph from rooms and exits (build) and compares the result with the disc (recompute).
Run python3 -I tools/port/scene/sgp.py dump FILE.SGP --connections to include connections.
Loading¶
ZEngineDataBase::AllocSequencecallsLoadSoundGraph(enginedatabase.cpp:1070, defined at 1491). If the member's size is greater than 0, it reads the whole file and passes it toZDllSound::InstallSoundGraph(sound.cpp:856). That function creates aZSoundGraphand keeps the buffer.- After the geoms exist,
CreateSoundGraph(enginedatabase.cpp:1206, defined at 1503) callsZSoundGraph::Initialize(soundgraph.cpp:121). It reads the five counts, copies each table into its own heap array and walks every group under the root (RecurGetNextGroup). For eachZROOMother than the root, it setsZROOM::m_lSoundGraphId(zroom.h:224, default -1) withGetSoundGraphIndex(CalcTotalName(false), names, size)(soundgraph.cpp:96). Then it frees the file buffer. - Without an SGP, or with a zero-length one,
m_pSoundGraphstays 0. Every sound is then audible with no occlusion (sound.cpp:1151). However,NotifyActorsreturns at once (eventcontroller.cpp:248), so NPCs hear no sound events at all.
Layout¶
Little-endian. Each table directly follows the one before it, with no alignment.
| Offset | Field | Read by |
|---|---|---|
| 0x00 | m_lNumExits, m_lNumExitPairs, m_lNumRooms, m_lNumConnections, m_lNumLookupEntries (5 × u32) |
Initialize 125–129 |
| 0x14 | SExit[m_lNumExits], 0x5c bytes each |
131–135 |
| … | SExitPair[m_lNumExitPairs], 12 bytes each |
136–140 |
| … | SGRoom[m_lNumRooms], 12 bytes each |
141–145 |
| … | uint32 m_pConnections[m_lNumConnections] |
146–150 |
| … | uint32 m_pExitLookup[m_lNumLookupEntries] |
151–155 |
| … | name table up to the end of the file: char name[] (NUL-terminated), then a u32 graph room index (unaligned, assembled bytewise) |
GetSoundGraphIndex 96–111 |
The structs are in soundgraph.h.
SExit (0x5c). One portal between two graph rooms.
| Off | Field | Meaning |
|---|---|---|
| 0x00 | float m_fOpenness |
1 open … 0 closed. 1.0 in every file on the disc. ZOpeningBase::AdjustOpenness (doors.cpp:170) writes it through ZDllSound::AdjustExit (sound.cpp:827). |
| 0x04 | float m_fAmount |
Always 0. Never read on PS2. |
| 0x08 | int32 m_lFrequency |
Always 0. Never read on PS2. |
| 0x0c | uint32 lRoomId |
Graph index of the lower room. lRoomId < lRoomId2 in all 1,510 exits. |
| 0x10 | uint32 lRoomId2 |
Graph index of the higher room. |
| 0x14 | V3 vPlanePos |
Centre of the portal (see "Derived values"). |
| 0x20 | V3 vPlaneNormal |
Unit normal, (p2-p1)×(p3-p1). |
| 0x2c | V3 Verts[4] |
The corners p1..p4 of the lower room's ZROOM::ZExit for this portal. In 1,362 of 1,362 checked exits they are bit-identical to that ZExit in the .BUF. The higher room's ZExit has its own corners. |
GetGateVertices (soundgraph.cpp:173) returns the corners in reverse order and leaves the normal as stored when it is called for lRoomId with bNeg false. For the other room, or with bNeg true, it returns them in stored order and negates the normal.
SExitPair (12): m_lExitIdx1, m_lExitIdx2, float m_fDistance. This is a two-portal route through one room. Both exits belong to that room, and m_fDistance = |vPlanePos1 − vPlanePos2|. GetRoomExit (:664) picks the exit that belongs to a given outer room.
SGRoom (12): m_lExitLookupIdx, m_lConnectionCount, m_lConnectionsIdx. A room's lookup run is m_pExitLookup[m_lExitLookupIdx ..], one entry per ZROOM::ZExit slot in slot order. Its length is the next room's m_lExitLookupIdx minus this one, or the table end for the last room. The run must have exactly ZROOM::m_lNrExits entries, because GetExit(id, slot) (:675) indexes it by the ZExit slot. Doors (ZOpeningBase::AdjustOpenness via ZDllSound::AdjustExit, doors.cpp:240) and audiocontrollertype_roomcontrolled.cpp:107 call GetExit. An entry is the SExit index, or 0xffffffff for a slot whose ZExit lacks EXIT_CONNECTED (0x2). Checked: in 528 rooms located in the BUF, all 2,124 graph slots are EXIT_CONNECTED, and all 108 0xffffffff slots are unconnected. ZExit::m_lNeighborExitIndex agrees with the neighbour's run in 2,124 of 2,124 cases. Both index fields are running sums in room order.
Connection word: type << 30 | room << 16 | id. room is 14 bits (CONNECTION_ROOM_MASK 0x3fff) and id is 16 bits.
| type | ESoundGraphConnectionType |
id |
Route from room i to room |
|---|---|---|---|
| 0 | CONNECTION_TYPE_EXIT |
SExit index |
one portal |
| 1 | CONNECTION_TYPE_EXIT_PAIR |
SExitPair index |
two portals through the pair's room |
| 2 | CONNECTION_TYPE_VIA_ROOM |
a graph room k | three or four portals; tested as (k, i) and (k, room) |
Room i's run holds only connections to rooms with a higher index. It is sorted by (room, type), and duplicates are kept. Every lookup sorts its two rooms first (if (lId2 < lId1) swap). It then reads the run of the lower room and treats the first and last words as the run's minimum and maximum room.
Name table: one entry per graph room, in index order (indices 0..n−1 in all 50 non-empty files). Each name is ZGEOM::CalcTotalName(false): the group names from below the root down to the room, joined with \. An example is M01/M01_LOC_Ins_WineCellar.zip\M01_WineCellar_MainRoom_01a. Up to 177 characters appear on the disc. strcasecmp does the matching. A room whose name is missing keeps id −1.
Derived values and how to recompute them¶
build(rooms) in sgp.py implements these rules. recompute <corpus> applies it to the rooms and exits recovered from each disc file:
- Exit order. Walk the rooms in index order and each room's slots in order. A connected slot whose portal is new becomes the next
SExit, withlRoomIdset to the current room. The disc matches this order in 25 of 25 graphs, solRoomIdis always the lower room. vPlaneNormal/vPlanePos. These areZROOM::CalcExitNormaland the centre ofZROOM::CalcBoundFromExit(zroom.cpp:137–171): the bounding rectangle of the corners in thecreatematframe. They are bit-exact in 1,188 and 1,015 of 1,510 exits. The worst position differs by 0.084 units and the worst normal by 6e-8.- Exit pairs. For each room in order, take every two graph exits a, b of its run (a's slot first) and record the distance between their
vPlanePosvalues. The disc matches this in 25 of 25 graphs. The distance is computed in double and stored as a float. It is bit-exact in 7,574 of 8,181 pairs and 1 ulp off in the rest. Pairs whose two exits join the same two rooms are listed but never referenced (800 pairs). Every other pair is referenced exactly once. -
Connections. Do a depth-first walk from each room i, trying exits in slot order. A path never reuses an exit and never continues from a room it has already visited, but it may end in one. The walk stops at 4 exits. Each path that ends in a room j > i adds one entry:
- 1 exit gives type 0.
- 2 exits give type 1, with the pair of the middle room.
- 3 or 4 exits give type 2, with k =
rooms[n//2](the second room of a 3-exit path, the third of a 4-exit path). The entry is skipped if k = j.
Duplicates are kept, and the run is sorted by (j, type). This rule reproduces every room's connections as a multiset in 24 of 25 graphs (1,254 of 1,320 rooms), with exact word counts. The exception is
M09_main, where types 0 and 1 match but type 2 does not (12,855 entries on the disc against 98,574 by the rule). The order inside a (j, type) group matches in 770 of 1,320 rooms (see Unknown). Rebuilding the whole file is byte-identical for 48 of 71 files. The rest differ only in float rounding and within-group order.
How the engine uses it¶
GetAudiable(:202), every frame, for sources whose owner room differs from the listener's. Ifm_lNumConnections == 0, every source is audible. Otherwise it callsTestGraph(:432), which scans all of the pair's type 0 and 1 connections. A source is audible if any route is open (the product of openness for a pair). The most open route wins, and among equals the later one wins (<=). If all routes are closed, it takes the closest one. The route becomes anSPathfilter chain: occlusion per exit, plus an obstruction amount|cos|between the listener direction and the exit normal.ZDllSoundPS2sends(1 − openness)² × 16384to the IOP (soundps2.cpp:444). Type 2 entries are ignored here.- NPC hearing.
NotifyActorsandTraceSound(eventcontroller.cpp:244,:305) callTestRooms(listenerPos, eventPos, listenerRoom, eventRoom, flags, dist)(:711) for events insidem_fDistance. It collects up to 32 type 0 and 1 routes and tests whether the event position lies inside the frustum from the listener through each portal. Event flag bit 0 allows up tom_lMaxReflections(5) portals that fail the test; bit 1 includes closed exits. If both rooms have the same graph id (including −1 and −1), the event is heard. A room with id −1 hears nothing from other rooms. Type 2 is ignored. TestRooms(id1, id2, bProbe)(:583) servesZDllSound::TestGraph(sound.cpp:997) and the idle-speech check inhm3actor.si.cpp:1699. It examines only the first connection to the other room. A type 2 entry there re-tests (k, i) and (k, j).- Doors:
AdjustExit(room, slot, openness)goes throughGetExit. Environment morph (GetEnvMorph,:397) uses theZROOMexits, but only whenm_lNumConnectionsis non-zero.
Edge case: when the lower room has no connections, TestRooms still reads pConnections[lCCount − 1]. For room 0 that is the word before the array (M00_main rooms 0 and 1). A generator should give rooms without exits the highest indices.
Corpus¶
| Kind | Files | Content |
|---|---|---|
| empty | 21 | 20 zero bytes (menus, loaders, intros, save screen) |
| rooms only | 25 | no exits or connections: news (2 rooms), premission (10), M09_postmission (1 room, 5 unconnected slots) |
| full | 25 | 2 to 221 exits, 1 to 1,776 pairs, 5 to 158 rooms, 3 to 18,720 connections; at most 41 slots per room, 3,342 connections per room, 490 per room pair |
Totals: 1,510 exits, 8,181 pairs, 115,394 connections (1,510 type 0, each exit exactly once; 7,381 type 1; 106,503 type 2). Files range from 20 to 128,422 bytes. All type 0 and type 1 entries are consistent with their exits' rooms.
What a new scene needs¶
- Minimum (the menu scenes' choice): 20 zero bytes. A graph exists, every room keeps id −1, every sound is audible without occlusion, and NPCs hear every event within its distance, through walls. Do not leave the member out, or NPCs become deaf (see Loading).
- One room:
m_lNumRooms = 1, oneSGRoom {0, 0, 0}, a lookup run ofm_lNrExits×0xffffffff, and the name entry"<CalcTotalName>\0" + u32 0. This is whatM09_postmissiondoes. Behaviour is the same as the minimum, and doors find no exit. - Real occlusion and hearing: use
sgp.build([RoomSpec(name, [ExitSlot(corners, neighbour, neighbour_slot), …]), …]). Supply oneRoomSpecperZROOM, with exactly itsZExitslots in order. A slot is connected when itsZExithasEXIT_CONNECTED. Its corners come from theZExit, and its neighbour is the graph index ofm_pNeighbortogether withm_lNeighborExitIndex. The input is the level's rooms and portals, not walkable geometry. Those must agree with the roomZExitarrays in the.BUF, which the room'sExitsOffsetproperty points to (ZROOM::SetExitsOffset,zroom.cpp:521), and with the.RMC/.RMIroom trees.buildderives everything else: planes, pairs, connections and the name table. A room's graph index only orders the tables; rooms are matched by name. Room counts must stay below 16,384 and exit and pair counts below 65,536.
AI data a level needs (not in the SGP)¶
- Path graph:
SPackedGeomsHeader::m_lOffsetPathfinder4Datain the.GMSpoints to a u32 length and then a block.InitPathfinder4Data(enginedatabase.cpp:978) passes it toPF4::CreatePathFinderand thenPF4RunTime::ZData::LoadDataBlock(pf4runtime.cpp:681). The block has 14int16counts, then split trees, height trees, plane equations, components, graphs, nodes, vertices, links (ZLink: cost, node, graph, type exit/action, key mask,m_rDoorController), exit distances, corners, sub-nodes and static obstacles (pf4runtime.h). The same data also drivesZEntityTracker.tools/port/scene/gms.pyparses it. - Patrol paths (
eScriptPathTypeLOOPING/PINGPONG,pathfollower.si.h), door markers (ZPathfinderDoorMarker) and cover are geoms with properties in the.GMS/.PRP. They do not refer to the SGP.
Unknown¶
- Order within a (room, type) group. It matches the walk in only 770 of 1,320 rooms. No stable, MSVC
qsort, VC6std::sort, BFS or reverse-order variant did better. One example: HitmanBloodMoney room 0 lists its exit-pair block for exit 2 first, then exit 1, then exit 0, and moves two entries to the end. That looks like an unstable sort over a walk order that was not reproduced. The order matters only for ties inTestGraphand for the first-entry rule ofTestRooms(id1, id2, bProbe). M09_maintype 2 table. It cannot be reproduced. On the disc, 630 reachable room pairs have no entry, and the per-pair and per-room counts are lower than the rule gives. The cause may be a stale table or a different limit.- Room order. For 34 of 50 files the order is the reverse of the
.GMSorder of class 0x100021 geoms. In the main levels, other room classes (0x10002e, 0x10003f, 0x200002, the map and backdrop rooms) come first. The general rule is not established. - Float rounding. The tool's precision for
vPlanePos,vPlaneNormalandm_fDistanceis not known (see the counts above). m_fAmountandm_lFrequency. Always 0, and never read on PS2. They may be PC/EAX leftovers.- Path limit. Why the limit is 4 exits. It is not
m_lMaxReflections, which is 5.