Files
ClassicAPI/src/model/SceneTerrain.cpp
T
brues-code 6b907f09d4 feat(model): scene:SetWorldTerrain, a map's ground, water and sky in a ModelScene (#139)
* fix(dbc): accept id 0 in DBC::Record, as the client's own lookup does

The client indexes a table's id map (store +0x08) by id and refuses only an
id above the largest id (+0x0C, signed, so negatives too) and an empty slot
(0x005F4010: test/jl, cmp/jg, null test). DBC::Record also refused id 0,
which is a real row in Map.dbc (Eastern Kingdoms), TerrainType and
ItemClass (Consumable); every other table leaves slot 0 empty, so it still
returns null there.

Effects: map 0 now has its directory, so scene:SetWorldTerrain(0, ...)
finds Eastern Kingdoms' tiles; the scene's death thud plays on TerrainType
0; ItemClass 0 resolves to "Consumable" in the item name lookups.
Instance::Info and the C_Map MapInfo reader had each copied the lookup
without the id 0 check to work around it; both now call DBC::Record.

* feat(model): scene:SetWorldTerrain, a map's ADT terrain in a ModelScene

scene:SetWorldTerrain(mapID, x, y [, radius]) shows the map's ground around
a point, in map coordinates (radius default one tile, 533.33331 at
0x0080654C); scene:ClearWorldTerrain() removes it. The Gurubashi Arena's
pit is terrain under its WMO, so SetWorldModel at the arena's MODF plus
SetWorldTerrain shows it as the world does.

The chunks are the world's CMapChunk objects, owned by the scene and built
and drawn by the world's own functions (model/SceneTerrain.cpp):

- Load: the ADT is read whole and its MHDR read as ParseAreaFile
  (0x006C2010) reads it. LoadAreaTextures (0x006C20E0) fills an area
  stand-in's texture slots. Each chunk is constructed (0x006AF1D0), gets
  CreateAreaChunk's fields (0x006C2880) and the self-contained steps of
  CMapChunk::Load (0x006AF5F0) in order: FixupRecordPointers, header
  fields, BuildVertices, LoadNormals, the impassable flag, AddLayer,
  CreateShadowTexture, CreateCombinedTexture. LinkNeighbours (global area
  table), LoadReferences (the world's doodad hashes), LinkLights and the
  area's filing are world state and left out; the scene links its own
  chunks by LinkNeighbours' rule. Each record is bounds-checked first.
- Buffers: the world's chunk pool is freed and rebuilt on a far clip change
  or map unload (CreateChunkBufferPool 0x006AE640), and only the world's
  listed chunks give theirs back; its empty-pool fallback is the device's
  shared dynamic buffer. The scene makes its own static pools the same way
  (CreatePool 0x0058A160, PoolBuffer 0x00589F80; freed by 0x0058A040 and
  GxBufferRelease), which SelectMesh never trades away (it re-acquires only
  for a usage 2 pool).
- Draw, before the WMO: DrawChunkPass (0x00684510) per chunk the scene's
  volume shows (IsBoxCulled 0x00682F40): the chunk level and view depth
  (0x006AFAD0), the world transform at the chunk less the eye, a lighting
  record at the chunk's centre with the scene's lights there, the scene's
  fog and, when the depth less twice the radius is inside the fog end, its
  light as the sun (DrawMapEntity's slots 0x006C0CC0), then SelectMesh
  (0x006BEB50). DrawLayers reads the camera (0x00C7CF20) and fog end
  (0x00C62964) globals, which hold the scene's values while it draws. The
  anisotropic mip bias is the pass's own immediate (0x00684528).
- Teardown: UnloadChunk (0x006A6B10) and ~CMapChunk, the scene's buffers,
  then each tile's texture handles (HandleClose, as UnloadArea does).
- The chunks are built again when the terrain shader flags, shadowLevel or
  map shadows change (glue screen to world).

Not yet: collision, the tile's doodads and grass, terrain liquid.

* feat(model): actors walk on the scene's terrain; traces and sweeps find it

The scene's ground is now its WMO and its terrain, taken in the order the
world's queries take map objects and chunks:

- Box query (the mover's sweeps, SweepCollider): QueryGroundFacets gathers
  the WMO's facets, then the terrain's, as 0x006AA8B0 does; a WMO still
  loading fails it. The terrain leg is the gather's cell math (0x006AA92D:
  fistp(float(0.24 x float(17066.666 - c)) - 0.5), bounds against 0 and
  34133.332) and GatherChunkFacets' wall and triangle legs (0x006AAE2A..
  0x006AB3F1): AddImpassableWalls' quads (0x006AB530, the 32000 immediate
  at 0x006AB59C), the hole bits (0x008103F8), the five-vertex outcodes
  against the box grown by 0.019444443 (0x00810468, 0x008101B8) and the
  four cell triangles (0x00810438). Their planes are Contact::MakeFacet,
  whose arithmetic is the chunk gather's inline SSE path operand for
  operand. GatherChunkFacets itself finds its chunk through the global
  area table (0x00C96318), so it cannot see the scene's chunks.
- Segment trace (TraceSegment, area-kit shards): TraceGround traces the WMO,
  then lets the terrain lower the fraction, as 0x0069BFF0 does. The terrain
  leg converts the ends to cells as 0x0069C320 does and calls the client's
  four cell walks (0x0069C4E0, 0x0069C570, 0x0069C600, 0x0069C780), which
  only fill its scratch list (0x00C962C8, count 0x00C89F40); then
  TraceListedCells (0x0069C920) over the scene's chunks: the ray relative
  to each chunk, its four cell triangles (0x0081047C) through
  IntersectRayTriangle (0x007C2C40, slack immediate at 0x0069CB96), the
  nearest t*k at or above 0. The normal is PlaneFromTriangle's (as the
  terrain first hit 0x006AA600 takes it), turned toward the start.

The mover's facet context is the scene frame. World::Sweep moved to the
scene (SweepGround) so it sweeps against both. A cell outside the scene's
chunks has no ground; the world fails or ends there, but the scene's
terrain ends at its radius and a WMO may reach past it.

* feat(model): shadows, selection circles, markers and reticles on terrain

The world lays a decal through its box test TestWorldBox (0x006AD2C0): map
objects into the shared triangle batches (mask 0xF00F0), then the terrain
(0xF0F00, TestTerrainBox 0x006AD4E0). The terrain leg's cells are
fistp(float(0.24 x d) - 0.5) of the distances from the map origin (0.24 at
0x00810B44, 0.5 at 0x0086AAC4), the first from the unrounded origin - min.y;
rows run from the max-x cell >> 3 to the min-x cell >> 3, columns likewise.
TestChunkBox (0x006AD610) finds each chunk through the global area table,
takes the box less the chunk position, clips the cell rectangle to
{0, 0, 7, 7} (IntersectCellRects 0x006ADBF0) and, under 0x100, calls
TestChunkTriangles (0x006AD7E0, fastcall(chunk, rect, box, unused), ret 8),
which reads only the chunk and writes one batch: its kept cell triangles,
the chunk's translation as the matrix, its vertices at +0x83C.

Terrain::CollectDecalBatches is that leg over the scene's own chunks,
calling TestChunkTriangles on each. World::DrawDecal now takes the WMO and
the terrain (either null), collects them in the world's order and draws
the batches with the existing DrawDecalBatches states. The scene's decal
helpers take the paint's ground (WMO, terrain); the flat quad is left for
a scene with neither.

* feat(model): footsteps and the actor's ground on the scene's terrain

AttachEntityToGround (0x006A8A20) traces the terrain (TraceTerrainSegment,
mask 0x100) and the map objects down 1000 yards from the probe point; with
neither hit it probes up past the bounds; the terrain wins when it is
nearer. A terrain win links the unit outdoors (the sun, no map object
lights), and its ground id is GetGroundEffect (0x0069B1C0) at the entity's
position: the cell from the map origin (0.24 at 0x0081073C, 0.5 at
0x0086A750; distances 0 to 34133.332), no hole, then the cell's dominant
layer from the chunk's MCNK +0x40 map (the row's word masked by
0x008104B4[column] and shifted by 0x008104C4[column]), that layer's effect
id (+0x0A, 0xFFFF none) into GroundEffectTexture (0x00C0DC60 / 0x00C0DC64),
whose +0x18 is the TerrainType id. GetGroundEffect finds the chunk through
the global area table, so Terrain::GroundEffect transcribes it over the
scene's chunks.

World::Place now runs both legs (the terrain through Terrain::Trace), and
World::Settle / Ground take the backdrop (nullable) and the terrain, so a
scene with terrain and no WMO places its actors too; the cached placement
keys on the terrain's serial as well. The Gurubashi pit's layer (ground
effect 587) is TerrainType 0, Dirt.

* feat(model): the terrain's own doodads (trees, plants, rocks) in a scene

A chunk's MCRF doodad references index the tile's MDDF placements, and the
world makes one CMapDoodadDef per unique id (+0x04, the hash 0x00CA7DDC).
PlaceDoodadDef (0x00694BC0) places it: position (origin - +0x10,
origin - +0x08, +0x0C), each a float sum; scale the word +0x20 x 1/1024
(0x008090D8); angles float(degrees x pi/180) with z + pi in one store;
the matrix TranslateMatrix, RotateMatrix about z, y and x (0x007BDD60),
ScaleMatrix (0x007BDD00); then CreateDoodadModel (0x00695100), the same
model setup as a WMO's doodads. LoadReferences (0x006B00F0) marks a chunk's
doodads outdoors (flag 4, intensity 1.0), and map lights only reach doodads
owned by WMO groups, so they take the sun and the zone fog: in the scene,
the frame's light and fog.

The WMO doodad code is now shared: a Doodad's WMO is optional and its sound
flag points to its owner's. World::Props holds a map's own doodads with the
same model setup, bounds, distance fade (0x00683F80), animate list, sounds
and collision facets (GatherDoodadFacets 0x006ABC40 rules, one stamp per
query). Terrain::QueryFacets adds each chunk's props after its triangles,
as GatherChunkFacets does (0x006AB496); a prop still loading fails the
query. SetPlayWorldModelSounds covers them.

* feat(model): the terrain's grass (detail doodads) in a scene

The world's chunk pass (0x006847C9) scatters a drawn chunk's detail
doodads once, with world flag 0x100000, when its view depth +0x78 is below
the horizon 0x00867958 (read live; 300.0 in this image):
CMapChunk::CreateDetailDoodads (0x006BFC10) from the layers' ground
effects, seeded by the chunk's grid position. The detail pass (0x006813D0)
then sets stage 0's mip bias (the immediate at 0x006813EA) and, for each
such chunk with a batch +0xC0, the chunk's transform and lighting record,
then DrawDetailDoodads (0x006C02D0) with the fade ramp. The scene runs both
over its own chunks.

The batches' slot buffers come from the world's detail pools, which
PrepareDetailDoodadBuffers (0x006B1D30) frees and rebuilds while 0x00CB0034
is set (a frillDensity change), after freeing only the world's listed
chunks' batches (0x006924F0); a slot's release (0x006B21D0) reads its
buffer's pool. So a scene batch gives its slot buffers back after each draw
(the world's idle release 0x006B28E0); the scene runs Prepare itself while
the flag is set (the world's frame never does at the glue screen), and
frees its batches (0x006B2030) to scatter them again when the rebuild
stamp 0x00CB003C, written only by Prepare, moves. CreateDetailDoodads reads
512 bytes at the MCSH pointer with or without a shadow map, so a chunk
whose range lies outside its file gets none.

The chunk pass and the detail pass each get their own render state push,
as DrawChunkPass and the detail pass do: the shader path's layer draw
(DrawLayersShader 0x006BF690) leaves its pixel program bound, and with one
push around both the grass drew through it as opaque quads.

* feat(model): the terrain's water and lava in a scene

A scene's terrain now shows the rivers, lakes, ocean and lava of its
chunks, drawn by the world's own liquid code.

Load: after LoadNormals, each chunk runs CMapChunk::LoadLiquids
(0x006AF760), which reads one MCLQ block (0x324 bytes) per liquid flag
of the MCNK header into the four slots at chunk +0x118 and builds each
slot's vertices. The record check now bounds the MCLQ blocks too.

Buffers: the slots take a vertex and an index buffer from two static
pools the scene makes, as CreateLiquidBufferPool (0x0068CA50) makes the
world's (81 vertices of 40 bytes, 192 indices). The world rebuilds its
own pool, which would leave the scene's slots on freed buffers;
CChunkLiquid::Draw (0x0068DE40) re-acquires only for a usage-2 pool.
Each pair is marked unfilled with 0x0058A130, the call
AcquireLiquidBuffers (0x0068CC60) makes, so the first draw fills the
vertices and the strip.

Draw: the three passes are copied in the world's order. Magma
(DrawMagmaPass 0x006855A0: no culling, lighting or blending) after the
WMO, as DrawWorldScene has it; the ocean (0x00685010) then the river
(0x006851B0) before the WMO's liquids, as RenderLiquid (0x006816D0)
has it. Each pass binds its swatch, then the water program
(0xC81B7C) with the white specular and the shininess (0x8102E8) and
alpha blend when fancy water (0xC9A324) is on, else the add combine
on stage 1. A slot is culled by its box (0x0068DF40: the chunk's box
with the slot's heights as z), placed at its chunk less the eye, lit
as GatherSlotLights (0x0068DF70) lights it, and drawn with the pass.
The camera, fog end, frustum and world stack are staged as for the
chunks (one Staged helper now serves all three).

Colours: the river and ocean swatches refill from the light state
while their fresh flags (0xC8117C, 0xC81B70) are clear. World::MapWater
swaps the eight water fields of the live light record (+0x34..+0x40,
+0x68..+0x74) for the scene map's at the eye; the flags are cleared
before and after the draw, so the swatches take the scene's colours
and the world's next frame refills its own. The WMO's water uses the
same swap.

Actors do not swim in this water yet, and GetLiquidAtPoint does not
find it.

* feat(model): swimming, liquid queries and the camera under terrain water

The scene's terrain liquid now answers the queries the world asks of
its own, so actors swim in it, GetLiquidAtPoint finds it, the camera
goes under it, and the splashes play.

Point query: GetLiquidStatus (0x0069B6D0) finds its chunk through the
global area table, so its terrain leg is transcribed over the scene's
chunks: u and v at 0.24 cells a yard (0x0081073C), stored and rounded
less 0.5; each slot's tile byte (+0x10) gives type & 3; the ocean is
height 0 under 0.01, else the MCLQ heights (+0x0C) bilinear at 53
bits, stored, the point under it plus 0.01. The scene asks the WMO
first and the terrain after, as GetLiquidStatus asks the map objects
first; GetLiquidAtPoint and the actor's swim step use it.

Swimmer's facets: GatherChunkFacets (0x006AADC0) under class 0x30000
adds an impassable chunk's walls (they ignore the class) and, for
each slot the class names, GatherLiquidFacets (0x006AB4E0): the facet
batches reset, GatherLiquidCellFacets (0x0068CEC0, called on the
scene's own slots) with the chunk-relative box and clipped cell
rectangle, and the batches emitted. The doodad leg takes no def for
this class. The box-to-cells step and the chunk window are shared
with the solid query; ResetFacetBatches and EmitFacetBatches leave
SceneWorld's anonymous namespace for it.

Camera: CameraEnvironment (0x006809C0) asks the claimed group's
liquid, else the liquid query (0x006723D0) at the probe point: the
eye lowered to the lowest near corner of the view. The probe drop
now comes from the scene's paint, so a scene without a WMO has one.
Under a liquid the world builds its light table with that liquid's
params and takes its fog from it (BuildSceneFog 0x006CEE30: end
+0x44 capped at the far clip, start +0x48 times it, colour band 7
+0x1C); LiquidFog swaps the frame's fog for that during the paint,
and a WMO's lava or slime without its own fog targets the same. A
terrain-only scene clears to the fog colour under a liquid, as
DrawWorldScene does (0x0068124D); the paint order and the water
colours follow the camera's liquid.

Sounds: CGUnit_C::UpdateSwimState (0x006030C0) plays the race splash
(ChrRaces +0x34, kept at +0xB18 by PostInit 0x005FB880) when the depth
past 0.4 of the height (0x0080C5C0) differs from the last test's
(+0x9A4), while not swimming; the mover's port now does, and the
actor keeps its unit's splash. The $FSD footstep is wet
(IsPointAtLiquidSurface 0x0060A740) when the unit, placed outdoors,
stands in a liquid (UpdateMapEntityEnvironment 0x0069E280) whose
LiquidType for the area (0x007CCF40; area from 0x0069B120) has flag
0x2, and the event point is from 0.01 above to 2/3 below the surface.

Constants already named under other roles are reused: 0x0081073C and
0x0086A750 become VAR_POINT_CELLS_PER_YARD and VAR_POINT_HALF_CELL,
and the nameplate lift 0x0080ABFC becomes VAR_TWO_THIRDS.

* feat(model): the map's sky over a scene's terrain without a WMO

A scene with terrain and no WMO now clears and draws its frame as the
world's does in the open, in place of a depth-only clear.

DrawWorldScene (0x0068123F) clears to black while the camera is dry and
to the fog colour (+0x70) under a liquid, and draws the sky only when
dry and the weather byte is set; outside every map object that byte
always stands set. The sky pass the WMO scenes already run (the dome
colours from the camera's heading, the map's light from BuildMapLight
with its sun disc and skybox slots, DrawSky, the world's objects put
back after) now takes the map, the camera's liquid and the portal
walk's skybox weight instead of the backdrop, so ClearAndDrawOpenSky
runs it for the terrain's map with no skybox from a walk. Under a
liquid the frame's fog is already the map's for it (LiquidFog), so the
clear takes that colour.

* feat(model): the drift motes around a scene camera in a liquid

A scene's camera in water, ocean or magma now sees the world's drift
cloud: the motes that float around the camera under a liquid.

The world builds one cloud at start-up (0x0066F971: 0xFA40 bytes,
CreateDriftCloud 0x0068E5A0 with the size base 1/36, the edge 30.0 and
Textures\WaterPoop02.blp at 0x00867A34) and drives it from its own
camera. A scene now owns a cloud made the same way the first time its
camera enters a liquid, and runs the world's steps from its camera:

- CameraEnvironment's change leg (0x00680AEE): on a change to water or
  ocean the scatter (0x0068E720) in the new liquid's mode and the size
  base 1/36 (0x0068E670), 1/9 for magma, and the enable byte +0xFA14;
  slime clears it.
- The world frame's advect (0x0066FEE0 to 0x0068E930), with the scene's
  eye staged in the camera position (0x00C7CF20) and its frame time in
  the frame seconds (0x00C62510) the advect reads.
- The world stage's draw (0x006701E0 to 0x0068EFE0) after the models,
  in the device's view with the world transform identity.

Both run while the camera is in a liquid and world flag 0x2000000 (the
waterParticulates setting) is set. The cloud's texture is closed with
0x0068E640 when the scene goes. The module is model/SceneWater.cpp.

The world transform stack id 8 moves from two local constants to
Offsets.h (GX_XFORM_WORLD).

* feat(model): foam rings and wakes of actors in shallow water

An actor whose movement is on now leaves the world's ripples in shallow
liquid: foam rings while it stands, turns or splashes, wakes along its
motion while it moves.

The world keeps its ripples in one pool of 128 records (0x50 bytes) that
its liquid query fills and its liquid pass draws. A scene keeps a pool
of its own with the same records (model/SceneWater.cpp), and the steps
are transcribed:

- CGUnit_C::EmitWaterRipple (0x005FA760), each actor update (as
  0x0060802E) and after the swim decision's splash (kind 0xC9, as
  0x0060319F): with the unit's liquid and bit 0x40 (flag 0x1 of the
  area's LiquidType, kept now by the placement's liquid leg), the depth
  under twice the collision height (at least 1.0) and the next time
  (+0xC78) reached. The kind (moving 0xF, turning 0x30, still, splash)
  and the table 0x00860A4C {0, 0, 1, 0} make a wake or a ring; the size,
  lifetime and growth come from three draws of the client random stream
  and the function's own constants, scaled down past half the depth and
  for a still unit (0x0080C474); a ring takes a fourth draw for its
  heading, a wake the motion's; the next time follows the speed, or
  400 ms and a draw's share of 50.
- AddSplash's immediate leg (0x0068BDA0, gated by waterRipples
  0x0086A03C and mode 0x00C89DB8), AddRipple (0x0068F9F0: the active
  mover's range 0 to 31) and EmitRipple (0x0068F5C0): the record, its
  rise and decay at 0.4 of the life (0x00810348), and the liquid
  triangles TestWorldBox finds in its box with class 0xF0000, here
  World::LiquidTriangles over the WMO and the scene's terrain.
- UpdateRipples (0x0068FAE0), its ageing in the scene's update and its
  draw in the scene's liquid phase: blend 3, fog, depth write, culling
  and lighting off, the depth bias footstepBias x 1/1024, the world
  stack at Translate(-eye), per category its texture (0x00C800E4) over
  format 4 vertices with the alpha's byte over white and the uv through
  BuildDecalTransforms (0x006D6FA0) of the box turned by the heading.

The scene's terrain box test (TestTerrainBox) now has TestChunkBox's
liquid leg (0x006AD75E): GatherLiquidCellFacets on each slot the class
names, into the same batches. The shadow decal's class asks for liquid
too, so actor shadows lie on terrain water as in the world.

The scene's clock times the ripples, so a paused scene holds them.

* fix(model): scene terrain review fixes and shared view volume

SweepCollider in a scene with no WMO and no terrain returned the feet
again instead of feet + move: SweepGround copied the feet into the end
before its ground check. With no ground the body now moves the full
distance, as the doc says; while a WMO loads the feet still stay put.

Water::Update made the scene's ripple pool with a zero clock, so a
ripple added in the frame the camera first entered water died on the
next age. It now goes through Ensure, which sets the scene's clock.

Trace read 0x008029D4 under a second name, VAR_TRACE_RUN_EPSILON; it
now reads VAR_TWO_POW_MINUS_22 and the duplicate is gone.

The paint builds its view volume once and hands it to every pass that
built its own: the camera probe (0x006809E2), the portal walk for the
sky, the WMO draw and the terrain's chunk, magma and water passes. The
sky's draw (0x006D4940) only saves and restores the viewport, and
FrustumStore (0x00682D90) only copies from the volume, so one volume
serves the whole paint.

The SFile wrapper is one class (model/SceneFile.h) for the WMO and the
terrain readers. The terrain's decal and liquid box tests take their
chunk rectangle and shifted box from ChunkWindow, and AreaAt its row
and chunk from Cell and ChunkAt, in the same rounding as before.
2026-11-12 02:30:51 -06:00

1541 lines
74 KiB
C++

// This file is part of ClassicAPI.
//
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
// A ModelScene's terrain (scene:SetWorldTerrain, a ClassicAPI extension;
// research in docs/notes/model-scene.md "Extension 9").
//
// The chunks are the world's CMapChunk objects, owned by the scene, built
// and drawn by the world's own functions:
//
// - Load. The ADT is read whole into the scene's buffer and its MHDR read
// as ParseAreaFile (0x006C2010) reads it. The tile's textures load into
// an area stand-in through LoadAreaTextures, which touches only the
// texture slots. Each chunk is constructed by CMapChunk's constructor
// and gets CreateAreaChunk's fields (0x006C2880) and the self-contained
// steps of CMapChunk::Load (0x006AF5F0) in its order: record fixup,
// header fields, vertices, normals, the impassable flag, layers, shadow
// texture, combined texture, flags. Load's other steps write world
// state and are left out: LinkNeighbours reads the global area table
// (the scene links its own chunks by the same rule), LoadReferences
// makes doodad and map object defs in the world's hashes, LinkLights
// takes the world's map lights, and the area is never filed in the
// global table. Liquids and sound emitters (a null callback in 1.12)
// are left out too.
// - Buffers. The world's chunk buffer pool is freed and rebuilt on a far
// clip change and a map unload (CreateChunkBufferPool 0x006AE640), and
// only the world's listed chunks give theirs back first. The scene's
// chunks take their buffers from a static pool of their own, made as
// the world makes its pool; SelectMesh goes to the world's pool only for
// a buffer of the device's dynamic pool (usage 2).
// - Draw. DrawChunkPass (0x00684510) for each chunk the scene's view
// shows: the chunk level and view depth (0x006AFAD0), the box against
// the scene's volume (CullSceneChunks 0x00683BF0, without the horizon),
// the world transform at the chunk less the eye, a lighting record at
// the chunk's centre with the scene's lights there, the fog and, nearer
// than the fog end by twice the radius, the light (the chunk's slots
// 0x006C0CC0: the scene light stands in for the sun, the scene fog for
// the zone's), then SelectMesh. DrawLayers reads the camera and the fog
// end from world globals, so both hold the scene's values while it draws.
// - Settings. A chunk's textures are made for the shader flags, the
// shadowLevel and the map shadows flag it was built with; when any
// changes (a world loads after the glue screen), the chunks are built
// again from the kept files.
#include "model/Scene.h"
#include "Game.h"
#include "Offsets.h"
#include "dbc/Lookup.h"
#include "gx/Device.h"
#include "gx/Matrix.h"
#include "model/M2.h"
#include "model/SceneContact.h"
#include "model/SceneFile.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <map>
#include <unordered_map>
#include <memory>
#include <utility>
#include <vector>
namespace Model::Scene::Terrain {
namespace {
using Gx::Identity;
using Gx::Matrix;
using Gx::Translate;
constexpr char kVertexPoolName[] = "ModelScene_terrain_vtx";
constexpr char kIndexPoolName[] = "ModelScene_terrain_idx";
constexpr char kLiquidVertexPoolName[] = "ModelScene_liquid_vtx";
constexpr char kLiquidIndexPoolName[] = "ModelScene_liquid_idx";
struct alignas(16) ChunkMemory {
uint8_t bytes[Offsets::CMAPCHUNK_SIZE];
};
struct alignas(16) AreaMemory {
uint8_t bytes[Offsets::CMAPAREA_SIZE];
};
uint32_t U32(const uint8_t *at) {
uint32_t v;
std::memcpy(&v, at, sizeof v);
return v;
}
float F32(const uint8_t *at) {
float v;
std::memcpy(&v, at, sizeof v);
return v;
}
// What a chunk's textures were made for (AddLayer, CreateShadowTexture,
// CreateCombinedTexture, LoadAreaTextures).
struct Settings {
uint32_t shader = 0;
uint32_t shaderB = 0;
uint32_t shadowLevel = 0;
uint32_t mapShadows = 0;
static Settings Current() {
Settings s;
s.shader = Game::Read<uint32_t>(Offsets::VAR_TERRAIN_SHADER_FLAG);
s.shaderB = Game::Read<uint32_t>(Offsets::VAR_TERRAIN_SHADER_FLAG_B);
s.shadowLevel = Game::Read<uint32_t>(Offsets::VAR_SHADOW_LEVEL);
s.mapShadows = Game::Read<uint32_t>(Offsets::VAR_WORLD_FLAGS) & Offsets::WORLD_FLAG_MAP_SHADOWS;
return s;
}
bool operator==(const Settings &o) const {
return shader == o.shader && shaderB == o.shaderB && shadowLevel == o.shadowLevel &&
mapShadows == o.mapShadows;
}
};
// One ADT: its bytes (every chunk's record and the fill callbacks' alpha
// and shadow maps point into them), the MTEX names and the area stand-in
// that holds the tile's texture handles.
struct Tile {
std::vector<uint8_t> file;
uint32_t column = 0;
uint32_t row = 0;
const char *names = nullptr;
uint32_t namesSize = 0;
std::unique_ptr<AreaMemory> area;
// The MCIN indices of the chunks inside the radius.
std::vector<uint32_t> wanted;
// The model tables (MMDX names, MMID offsets, MDDF placements), or null.
const char *modelNames = nullptr;
uint32_t modelNamesSize = 0;
const uint32_t *modelOffsets = nullptr;
uint32_t modelCount = 0;
const uint8_t *placements = nullptr;
uint32_t placementCount = 0;
};
struct Chunk {
std::unique_ptr<ChunkMemory> memory;
Tile *tile = nullptr;
uint32_t index = 0; // row * 16 + column in the tile
std::vector<uint32_t> props; // its MCRF doodads, as indices into the land's props
// CreateDetailDoodads reads 512 bytes at the MCSH pointer whether or not
// the chunk has a shadow map; false when they lie outside the file.
bool detailSafe = false;
void *object() { return memory->bytes; }
};
} // namespace
struct Land {
std::vector<std::unique_ptr<Tile>> tiles;
std::vector<Chunk> chunks;
void *vertexPool = nullptr;
void *indexPool = nullptr;
std::vector<std::pair<void *, void *>> buffers; // one pair per chunk, in order
// The liquid slots' buffers, one pair per slot in chunk and slot order.
void *liquidVertexPool = nullptr;
void *liquidIndexPool = nullptr;
std::vector<std::pair<void *, void *>> liquidBuffers;
uint32_t mapID = 0;
Settings built;
// The chunks by (global row, global column), as the world finds them in
// its area table.
std::map<std::pair<uint32_t, uint32_t>, void *> byCell;
// Each chunk object's props, for the box query.
std::map<const void *, const std::vector<uint32_t> *> propsOf;
World::Props *props = nullptr;
uint32_t serial = 0; // this terrain among all loaded, for the actors' caches
uint32_t detailStamp = 0; // the detail pools' rebuild stamp the batches were made under
};
namespace {
uint32_t LevelCount(uintptr_t lists, uint32_t level) {
return Game::Read<uint32_t>(lists + level * Offsets::CHUNK_LEVEL_LIST_SIZE);
}
uint32_t FullVertexCount() { return LevelCount(Offsets::VAR_CHUNK_LEVEL_VERTEX_LISTS, Offsets::CHUNK_LEVEL_FULL); }
uint32_t FullIndexCount() { return LevelCount(Offsets::VAR_CHUNK_LEVEL_INDEX_LISTS, Offsets::CHUNK_LEVEL_FULL); }
// A byte of the file at an offset already checked to lie inside it.
const uint8_t *At(const std::vector<uint8_t> &file, uint64_t offset) {
return file.data() + static_cast<size_t>(offset);
}
bool Inside(const std::vector<uint8_t> &file, uint64_t offset, uint64_t size) {
return offset <= file.size() && size <= file.size() - offset;
}
// MHDR as ParseAreaFile reads it; false when a table lies outside the file
// or the tile has more names than the area holds.
bool ParseHeader(Tile &t) {
const std::vector<uint8_t> &f = t.file;
if (!Inside(f, Offsets::OFF_ADT_MVER_SIZE, 4))
return false;
const uint64_t mhdr = uint64_t{Offsets::ADT_MHDR_BASE} + U32(At(f, Offsets::OFF_ADT_MVER_SIZE));
if (!Inside(f, mhdr, Offsets::OFF_MHDR_MTEX + 4))
return false;
const uint64_t mcin = mhdr + U32(At(f, mhdr + Offsets::OFF_MHDR_MCIN)) + Offsets::MCNK_RECORD_HEADER;
const uint64_t mtex = mhdr + U32(At(f, mhdr + Offsets::OFF_MHDR_MTEX)) + Offsets::MCNK_RECORD_HEADER;
if (!Inside(f, mcin, uint64_t{Offsets::ADT_CHUNKS} * Offsets::MCIN_ENTRY_SIZE) || mtex < 4 || !Inside(f, mtex, 0))
return false;
const uint32_t size = U32(At(f, mtex - 4));
if (!Inside(f, mtex, size))
return false;
// LoadAreaTextures' walk: a name, then past its NUL, while inside the size.
uint32_t count = 0;
for (uint32_t i = 0; i < size; ++i) {
++count;
while (i < size && *At(f, mtex + i) != 0)
++i;
}
if (count > Offsets::CMAPAREA_TEXTURE_SLOTS || (size != 0 && *At(f, mtex + size - 1) != 0))
return false;
t.names = reinterpret_cast<const char *>(At(f, mtex));
t.namesSize = size;
// The model tables (0x006C2010): each offset points 8 bytes before its
// data, whose size is the dword before it. A tile whose tables do not
// fit keeps its ground and has no doodads.
const auto table = [&](uint32_t field, const uint8_t *&data, uint32_t &bytes) {
const uint64_t at = mhdr + U32(At(f, mhdr + field)) + Offsets::MCNK_RECORD_HEADER;
if (U32(At(f, mhdr + field)) == 0 || at < 4 || !Inside(f, at, 0) || !Inside(f, at, U32(At(f, at - 4))))
return false;
data = At(f, at);
bytes = U32(At(f, at - 4));
return true;
};
const uint8_t *names = nullptr, *offsets = nullptr, *placements = nullptr;
uint32_t namesBytes = 0, offsetBytes = 0, placementBytes = 0;
if (Inside(f, mhdr, Offsets::OFF_MHDR_MDDF + 4) && table(Offsets::OFF_MHDR_MMDX, names, namesBytes) &&
table(Offsets::OFF_MHDR_MMID, offsets, offsetBytes) &&
table(Offsets::OFF_MHDR_MDDF, placements, placementBytes)) {
t.modelNames = reinterpret_cast<const char *>(names);
t.modelNamesSize = namesBytes;
t.modelOffsets = reinterpret_cast<const uint32_t *>(offsets);
t.modelCount = offsetBytes / sizeof(uint32_t);
t.placements = placements;
t.placementCount = placementBytes / Offsets::MDDF_SIZE;
}
return true;
}
const uint8_t *McinEntry(const Tile &t, uint32_t index) {
const uint64_t mhdr = uint64_t{Offsets::ADT_MHDR_BASE} + U32(At(t.file, Offsets::OFF_ADT_MVER_SIZE));
const uint64_t mcin = mhdr + U32(At(t.file, mhdr + Offsets::OFF_MHDR_MCIN)) + Offsets::MCNK_RECORD_HEADER;
return At(t.file, mcin + uint64_t{index} * Offsets::MCIN_ENTRY_SIZE);
}
// The MCNK record of a chunk, or null when the record or a sub-record the
// build reads lies outside the file, or a layer names a texture the tile
// does not have.
const uint8_t *CheckedRecord(const Tile &t, uint32_t index, uint32_t textures) {
const std::vector<uint8_t> &f = t.file;
const uint32_t record = U32(McinEntry(t, index));
if (!Inside(f, record, uint64_t{Offsets::MCNK_RECORD_HEADER} + Offsets::MCNK_HEADER_SIZE))
return nullptr;
const uint8_t *header = At(f, record + Offsets::MCNK_RECORD_HEADER);
const auto sub = [&](uint32_t field, uint64_t size) {
return Inside(f, uint64_t{record} + U32(header + field) + Offsets::MCNK_RECORD_HEADER, size);
};
const uint32_t layers = U32(header + Offsets::OFF_MCNK_LAYER_COUNT);
if (!sub(Offsets::OFF_MCNK_MCVT, Offsets::MCVT_SIZE) || !sub(Offsets::OFF_MCNK_MCNR, Offsets::MCNR_SIZE) ||
layers > Offsets::MCNK_MAX_LAYERS || !sub(Offsets::OFF_MCNK_MCLY, uint64_t{layers} * Offsets::MCLY_ENTRY_SIZE))
return nullptr;
if ((U32(header + Offsets::OFF_MCNK_FLAGS) & Offsets::MCNK_FLAG_SHADOW) != 0 &&
!sub(Offsets::OFF_MCNK_MCSH, Offsets::MCSH_SIZE))
return nullptr;
// LoadLiquids reads one MCLQ block per liquid flag, back to back.
uint32_t liquids = 0;
for (uint32_t i = 0; i < Offsets::CHUNK_LIQUID_SLOTS; ++i)
if ((U32(header + Offsets::OFF_MCNK_FLAGS) & (Offsets::MCNK_FLAG_LIQUID_FIRST << i)) != 0)
++liquids;
if (liquids != 0 && !sub(Offsets::OFF_MCNK_MCLQ, uint64_t{liquids} * Offsets::MCLQ_SIZE))
return nullptr;
const uint64_t mcly = uint64_t{record} + U32(header + Offsets::OFF_MCNK_MCLY) + Offsets::MCNK_RECORD_HEADER;
const uint64_t mcal = uint64_t{record} + U32(header + Offsets::OFF_MCNK_MCAL) + Offsets::MCNK_RECORD_HEADER;
for (uint32_t i = 0; i < layers; ++i) {
const uint8_t *entry = At(f, mcly + uint64_t{i} * Offsets::MCLY_ENTRY_SIZE);
if (U32(entry) >= textures)
return nullptr;
if ((U32(entry + Offsets::OFF_MCLY_FLAGS) & Offsets::MCLY_FLAG_ALPHA) != 0 &&
!Inside(f, mcal + U32(entry + Offsets::OFF_MCLY_ALPHA), Offsets::MCAL_MAP_SIZE))
return nullptr;
}
return At(f, record);
}
uint32_t TextureCount(const Tile &t) {
uint32_t count = 0;
for (uint32_t i = 0; i < t.namesSize; ++i) {
++count;
while (i < t.namesSize && t.names[i] != 0)
++i;
}
return count;
}
void LoadTextures(Tile &t) {
t.area = std::make_unique<AreaMemory>();
std::memset(t.area->bytes, 0, sizeof t.area->bytes);
using Load_t = void(__thiscall *)(void *area, const char *names, uint32_t size);
reinterpret_cast<Load_t>(Offsets::FUN_LOAD_AREA_TEXTURES)(t.area->bytes, t.names, t.namesSize);
}
// UnloadArea's texture half (0x006A6AA0).
void CloseTextures(Tile &t) {
if (t.area == nullptr)
return;
uint8_t *area = t.area->bytes;
const uint32_t count = Game::Read<uint32_t>(area, Offsets::OFF_CMAPAREA_TEXTURE_COUNT);
for (uint32_t i = 0; i < count; ++i) {
void *&handle = Game::Ref<void *>(area, Offsets::OFF_CMAPAREA_TEXTURES + i * 4);
if (handle != nullptr) {
reinterpret_cast<void(__fastcall *)(void *)>(Offsets::FUN_HANDLE_RELEASE)(handle);
handle = nullptr;
}
}
Game::Ref<uint32_t>(area, Offsets::OFF_CMAPAREA_TEXTURE_COUNT) = 0;
t.area.reset();
}
// CreateAreaChunk's fields, then CMapChunk::Load's self-contained steps.
void BuildChunk(Chunk &c) {
using This_t = void(__thiscall *)(void *);
void *o = c.object();
std::memset(o, 0, Offsets::CMAPCHUNK_SIZE);
reinterpret_cast<void *(__thiscall *)(void *)>(Offsets::FUN_CMAPCHUNK_CTOR)(o);
Tile &t = *c.tile;
const uint32_t column = c.index % Offsets::TILE_CHUNKS;
const uint32_t row = c.index / Offsets::TILE_CHUNKS;
const uint32_t globalColumn = t.column * Offsets::TILE_CHUNKS + column;
const uint32_t globalRow = t.row * Offsets::TILE_CHUNKS + row;
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_COLUMN) = column;
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_ROW) = row;
Game::Ref<uint16_t>(o, Offsets::OFF_CMAPCHUNK_FLAGS) = 0;
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_INDEX) = c.index;
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_GLOBAL_COLUMN) = globalColumn;
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_GLOBAL_ROW) = globalRow;
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_COLUMN8) = globalColumn * 8;
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_ROW8) = globalRow * 8;
const uint8_t *record = At(t.file, U32(McinEntry(t, c.index)));
Game::Ref<const uint8_t *>(o, Offsets::OFF_CMAPCHUNK_RECORD) = record;
reinterpret_cast<void(__thiscall *)(void *, const void *)>(Offsets::FUN_CMAPCHUNK_FIXUP_RECORD)(o, record);
const uint8_t *header = Game::Read<const uint8_t *>(o, Offsets::OFF_CMAPCHUNK_HEADER);
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_AREA_ID) = U32(header + Offsets::OFF_MCNK_AREA_ID);
std::memcpy(Game::Ptr<uint8_t>(o, Offsets::OFF_CMAPCHUNK_POSITION), header + Offsets::OFF_MCNK_POSITION, 12);
Game::Ref<const uint8_t *>(o, Offsets::OFF_CMAPCHUNK_LOW_TEXTURES) = header + Offsets::OFF_MCNK_LOW_TEXTURES;
Game::Ref<const uint8_t *>(o, Offsets::OFF_CMAPCHUNK_DOODAD_MAP) = header + Offsets::OFF_MCNK_DOODAD_MAP;
reinterpret_cast<This_t>(Offsets::FUN_CMAPCHUNK_BUILD_VERTICES)(o);
reinterpret_cast<This_t>(Offsets::FUN_CMAPCHUNK_LOAD_NORMALS)(o);
reinterpret_cast<void(__thiscall *)(void *, uint32_t)>(Offsets::FUN_CMAPCHUNK_LOAD_LIQUIDS)(o, 1);
const uint32_t flags = U32(header + Offsets::OFF_MCNK_FLAGS);
Game::Ref<uint16_t>(o, Offsets::OFF_CMAPCHUNK_FLAGS) =
(flags & Offsets::MCNK_FLAG_IMPASSABLE) != 0 ? Offsets::CMAPCHUNK_FLAG_IMPASSABLE : 0;
using AddLayer_t = void(__thiscall *)(void *, void *area, const void *entry, const void *alpha);
const uint8_t *entry = Game::Read<const uint8_t *>(o, Offsets::OFF_CMAPCHUNK_MCLY);
const uint8_t *alpha = Game::Read<const uint8_t *>(o, Offsets::OFF_CMAPCHUNK_MCAL);
const uint32_t layers = U32(header + Offsets::OFF_MCNK_LAYER_COUNT);
for (uint32_t i = 0; i < layers; ++i, entry += Offsets::MCLY_ENTRY_SIZE)
reinterpret_cast<AddLayer_t>(Offsets::FUN_CMAPCHUNK_ADD_LAYER)(o, t.area->bytes, entry, alpha);
// Load 0x006AF6E5: the shadow needs the record's flag and map shadows.
if ((flags & Offsets::MCNK_FLAG_SHADOW) != 0 &&
(Game::Read<uint32_t>(Offsets::VAR_WORLD_FLAGS) & Offsets::WORLD_FLAG_MAP_SHADOWS) != 0) {
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_SHADOW_WORD) = U32(header + Offsets::OFF_MCNK_SHADOW_WORD);
reinterpret_cast<void(__thiscall *)(void *, const void *)>(Offsets::FUN_CMAPCHUNK_CREATE_SHADOW)(
o, Game::Read<const uint8_t *>(o, Offsets::OFF_CMAPCHUNK_MCSH));
} else {
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_SHADOW_WORD) = 0;
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_SHADOW_MAP) = 0;
}
if (Game::Read<uint32_t>(Offsets::VAR_TERRAIN_SHADER_FLAG_B) != 0 ||
Game::Read<uint32_t>(Offsets::VAR_TERRAIN_SHADER_FLAG) != 0)
reinterpret_cast<This_t>(Offsets::FUN_CMAPCHUNK_CREATE_COMBINED)(o);
Game::Ref<uint8_t>(o, Offsets::OFF_CMAPCHUNK_FLAGS) |= Offsets::CMAPCHUNK_FLAG_LIGHTS;
Game::Ref<uint8_t>(o, Offsets::OFF_CMAPCHUNK_FLAGS) |= Offsets::CMAPCHUNK_FLAG_FILED;
}
// LinkNeighbours' rule (0x006C29C0) over the scene's own chunks: a row
// before, a column after, a row after, a column before.
void LinkNeighbours(Land &land) {
std::map<std::pair<uint32_t, uint32_t>, void *> &at = land.byCell;
at.clear();
land.propsOf.clear();
for (Chunk &c : land.chunks) {
at[{Game::Read<uint32_t>(c.object(), Offsets::OFF_CMAPCHUNK_GLOBAL_ROW),
Game::Read<uint32_t>(c.object(), Offsets::OFF_CMAPCHUNK_GLOBAL_COLUMN)}] = c.object();
land.propsOf[c.object()] = &c.props;
}
const int steps[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
for (Chunk &c : land.chunks) {
void *o = c.object();
const uint32_t row = Game::Read<uint32_t>(o, Offsets::OFF_CMAPCHUNK_GLOBAL_ROW);
const uint32_t column = Game::Read<uint32_t>(o, Offsets::OFF_CMAPCHUNK_GLOBAL_COLUMN);
for (int i = 0; i < 4; ++i) {
const auto found = at.find({row + steps[i][0], column + steps[i][1]});
Game::Ref<void *>(o, Offsets::OFF_CMAPCHUNK_NEIGHBOURS + i * 4) =
found != at.end() ? found->second : nullptr;
}
}
}
// LoadReferences' doodad leg (0x006B00F0) for the scene's chunks: each MCRF
// doodad index into MDDF, one prop per unique id (the def hash 0x00CA7DDC).
// An index or a name outside the tile's tables is skipped.
void MakeProps(Land &land, void *frame) {
land.props = World::NewProps();
std::unordered_map<uint32_t, uint32_t> byUniqueId;
for (Chunk &c : land.chunks) {
const Tile &t = *c.tile;
if (t.placements == nullptr)
continue;
const uint8_t *record = At(t.file, U32(McinEntry(t, c.index)));
const uint8_t *header = record + Offsets::MCNK_RECORD_HEADER;
const uint32_t refs = U32(header + Offsets::OFF_MCNK_DOODAD_REF_COUNT);
const uint64_t list = uint64_t{U32(McinEntry(t, c.index))} + U32(header + Offsets::OFF_MCNK_MCRF) +
Offsets::MCNK_RECORD_HEADER;
if (!Inside(t.file, list, uint64_t{refs} * sizeof(uint32_t)))
continue;
for (uint32_t i = 0; i < refs; ++i) {
const uint32_t placement = U32(At(t.file, list + uint64_t{i} * sizeof(uint32_t)));
if (placement >= t.placementCount)
continue;
const uint8_t *entry = t.placements + static_cast<size_t>(placement) * Offsets::MDDF_SIZE;
const uint32_t nameIndex = U32(entry + Offsets::OFF_MDDF_NAME_INDEX);
if (nameIndex >= t.modelCount)
continue;
const uint32_t nameOffset = t.modelOffsets[nameIndex];
if (nameOffset >= t.modelNamesSize ||
std::memchr(t.modelNames + nameOffset, 0, t.modelNamesSize - nameOffset) == nullptr)
continue;
const uint32_t unique = U32(entry + Offsets::OFF_MDDF_UNIQUE_ID);
auto found = byUniqueId.find(unique);
if (found == byUniqueId.end())
found = byUniqueId
.emplace(unique, World::AddProp(*land.props, frame, t.modelNames + nameOffset, entry))
.first;
c.props.push_back(found->second);
}
}
}
void AttachBuffers(Land &land) {
for (size_t i = 0; i < land.chunks.size(); ++i) {
void *o = land.chunks[i].object();
Game::Ref<void *>(o, Offsets::OFF_CMAPCHUNK_VERTEX_BUFFER) = land.buffers[i].first;
Game::Ref<void *>(o, Offsets::OFF_CMAPCHUNK_INDEX_BUFFER) = land.buffers[i].second;
}
}
void *LiquidSlot(void *chunk, uint32_t slot) {
return Game::Read<void *>(chunk, Offsets::OFF_CMAPCHUNK_LIQUIDS + slot * sizeof(void *));
}
// The scene's liquid buffers onto its slots, in chunk and slot order, marked
// unfilled (a rebuilt slot fills them again).
void AttachLiquidBuffers(Land &land) {
using Invalidate_t = void(__fastcall *)(void *buffer);
size_t next = 0;
for (Chunk &c : land.chunks) {
for (uint32_t i = 0; i < Offsets::CHUNK_LIQUID_SLOTS; ++i) {
void *slot = LiquidSlot(c.object(), i);
if (slot == nullptr || next >= land.liquidBuffers.size())
continue;
const auto &pair = land.liquidBuffers[next++];
Game::Ref<void *>(slot, Offsets::OFF_CHUNKLIQUID_VERTEX_BUFFER) = pair.first;
Game::Ref<void *>(slot, Offsets::OFF_CHUNKLIQUID_INDEX_BUFFER) = pair.second;
reinterpret_cast<Invalidate_t>(Offsets::FUN_GX_BUFFER_INVALIDATE)(pair.first);
reinterpret_cast<Invalidate_t>(Offsets::FUN_GX_BUFFER_INVALIDATE)(pair.second);
}
}
}
void BuildAll(Land &land) {
land.built = Settings::Current();
for (auto &t : land.tiles)
LoadTextures(*t);
for (Chunk &c : land.chunks)
BuildChunk(c);
LinkNeighbours(land);
AttachBuffers(land);
AttachLiquidBuffers(land);
}
// The detail doodad batch of a chunk, freed (FreeDetailDoodadBatch); it holds
// no buffers between draws.
void FreeDetail(void *chunk) {
void *&batch = Game::Ref<void *>(chunk, Offsets::OFF_CMAPCHUNK_DETAIL_BATCH);
if (batch == nullptr)
return;
reinterpret_cast<void(__thiscall *)(void *)>(Offsets::FUN_FREE_DETAIL_DOODAD_BATCH)(batch);
batch = nullptr;
}
// UnloadChunk and the destructor, as DestroyChunk (0x006A6A00) does less
// the pool: the buffers are the scene's and stay; then each tile's
// textures.
void UnloadAll(Land &land) {
using This_t = void(__thiscall *)(void *);
for (Chunk &c : land.chunks) {
void *o = c.object();
reinterpret_cast<This_t>(Offsets::FUN_UNLOAD_CHUNK)(o);
reinterpret_cast<This_t>(Offsets::FUN_CMAPCHUNK_DTOR)(o);
}
for (auto &t : land.tiles)
CloseTextures(*t);
}
// The scene's pool, made as CreateChunkBufferPool makes the world's: one
// vertex and one index pool, a full-level pair of sub-buffers per chunk.
bool MakeBuffers(Land &land) {
using Pool_t = void *(__fastcall *)(uint32_t kind, uint32_t usage, uint32_t size, uint32_t arg, const char *name);
using Buffer_t = void *(__fastcall *)(void *pool, uint32_t itemSize, uint32_t count, uint32_t offset);
const uint32_t n = static_cast<uint32_t>(land.chunks.size());
const uint32_t vertexBytes = FullVertexCount() * Offsets::CHUNK_VERTEX_SIZE;
const uint32_t indexBytes = FullIndexCount() * Offsets::CHUNK_INDEX_SIZE;
land.vertexPool = reinterpret_cast<Pool_t>(Offsets::FUN_GX_CREATE_POOL)(
Offsets::GX_POOL_KIND_VERTEX, Offsets::GX_POOL_USAGE_STATIC, n * vertexBytes, Offsets::GX_POOL_ARG,
kVertexPoolName);
land.indexPool = reinterpret_cast<Pool_t>(Offsets::FUN_GX_CREATE_POOL)(
Offsets::GX_POOL_KIND_INDEX, Offsets::GX_POOL_USAGE_STATIC, n * indexBytes, Offsets::GX_POOL_ARG,
kIndexPoolName);
if (land.vertexPool == nullptr || land.indexPool == nullptr)
return false;
for (uint32_t i = 0; i < n; ++i) {
void *v = reinterpret_cast<Buffer_t>(Offsets::FUN_GX_POOL_BUFFER)(
land.vertexPool, Offsets::CHUNK_VERTEX_SIZE, FullVertexCount(), i * vertexBytes);
void *x = reinterpret_cast<Buffer_t>(Offsets::FUN_GX_POOL_BUFFER)(
land.indexPool, Offsets::CHUNK_INDEX_SIZE, FullIndexCount(), i * indexBytes);
land.buffers.emplace_back(v, x);
if (v == nullptr || x == nullptr)
return false;
}
return true;
}
// The liquid pools, made as CreateLiquidBufferPool (0x0068CA50) makes the
// world's: a vertex and an index pair for each of the slots.
void MakeLiquidBuffers(Land &land) {
using Pool_t = void *(__fastcall *)(uint32_t kind, uint32_t usage, uint32_t size, uint32_t arg, const char *name);
using Buffer_t = void *(__fastcall *)(void *pool, uint32_t itemSize, uint32_t count, uint32_t offset);
uint32_t slots = 0;
for (Chunk &c : land.chunks)
for (uint32_t i = 0; i < Offsets::CHUNK_LIQUID_SLOTS; ++i)
if (LiquidSlot(c.object(), i) != nullptr)
++slots;
if (slots == 0)
return;
const uint32_t vertexBytes = Offsets::LIQUID_VERTEX_SIZE * Offsets::LIQUID_VERTEX_COUNT;
const uint32_t indexBytes = Offsets::CHUNK_INDEX_SIZE * Offsets::LIQUID_INDEX_COUNT;
land.liquidVertexPool = reinterpret_cast<Pool_t>(Offsets::FUN_GX_CREATE_POOL)(
Offsets::GX_POOL_KIND_VERTEX, Offsets::GX_POOL_USAGE_STATIC, slots * vertexBytes, Offsets::GX_POOL_ARG,
kLiquidVertexPoolName);
land.liquidIndexPool = reinterpret_cast<Pool_t>(Offsets::FUN_GX_CREATE_POOL)(
Offsets::GX_POOL_KIND_INDEX, Offsets::GX_POOL_USAGE_STATIC, slots * indexBytes, Offsets::GX_POOL_ARG,
kLiquidIndexPoolName);
if (land.liquidVertexPool == nullptr || land.liquidIndexPool == nullptr)
return;
for (uint32_t i = 0; i < slots; ++i) {
void *v = reinterpret_cast<Buffer_t>(Offsets::FUN_GX_POOL_BUFFER)(
land.liquidVertexPool, Offsets::LIQUID_VERTEX_SIZE, Offsets::LIQUID_VERTEX_COUNT, i * vertexBytes);
void *x = reinterpret_cast<Buffer_t>(Offsets::FUN_GX_POOL_BUFFER)(
land.liquidIndexPool, Offsets::CHUNK_INDEX_SIZE, Offsets::LIQUID_INDEX_COUNT, i * indexBytes);
if (v == nullptr || x == nullptr)
break;
land.liquidBuffers.emplace_back(v, x);
}
}
// FreeChunkBufferPool's order (0x006AE5C0): each sub-buffer, then the pools.
void FreeBuffers(Land &land) {
using Free_t = void(__fastcall *)(void *buffer);
for (auto &pair : land.buffers) {
if (pair.first != nullptr)
reinterpret_cast<Free_t>(Offsets::FUN_GX_FREE_BUFFER)(pair.first);
if (pair.second != nullptr)
reinterpret_cast<Free_t>(Offsets::FUN_GX_FREE_BUFFER)(pair.second);
}
land.buffers.clear();
for (auto &pair : land.liquidBuffers) {
reinterpret_cast<Free_t>(Offsets::FUN_GX_FREE_BUFFER)(pair.first);
reinterpret_cast<Free_t>(Offsets::FUN_GX_FREE_BUFFER)(pair.second);
}
land.liquidBuffers.clear();
for (void *pool : {land.indexPool, land.vertexPool, land.liquidIndexPool, land.liquidVertexPool})
if (pool != nullptr)
reinterpret_cast<Free_t>(Offsets::FUN_GX_BUFFER_RELEASE)(pool);
land.vertexPool = land.indexPool = land.liquidVertexPool = land.liquidIndexPool = nullptr;
}
// The distance from a point to a chunk's square, its corner at the MCNK
// position and its sides one 16th of a tile toward the map's far side.
bool ChunkInRadius(const uint8_t *record, float x, float y, float radius, float side) {
const uint8_t *position = record + Offsets::MCNK_RECORD_HEADER + Offsets::OFF_MCNK_POSITION;
const float maxX = F32(position), maxY = F32(position + 4);
const float dx = x < maxX - side ? maxX - side - x : (x > maxX ? x - maxX : 0.0f);
const float dy = y < maxY - side ? maxY - side - y : (y > maxY ? y - maxY : 0.0f);
return dx * dx + dy * dy <= radius * radius;
}
int TileIndex(float origin, float coordinate, float tile) {
return static_cast<int>(std::floor((origin - coordinate) / tile));
}
} // namespace
Land *Load(void *frame, uint32_t mapID, float x, float y, float radius) {
if (!(radius >= 0.0f) || !std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radius))
return nullptr;
const char *dir =
DBC::StringField(Offsets::VAR_MAP_RECORDS, Offsets::VAR_MAP_COUNT, mapID, Offsets::OFF_MAP_DIRECTORY);
if (dir == nullptr || *dir == '\0')
return nullptr;
const float origin = Game::Read<float>(Offsets::VAR_MAP_ORIGIN);
const float tile = Game::Read<float>(Offsets::VAR_TILE_SIZE);
const float side = tile / static_cast<float>(Offsets::TILE_CHUNKS);
const int last = Offsets::MAP_TILES - 1;
const auto clamp = [last](int v) { return v < 0 ? 0 : (v > last ? last : v); };
const int rowFirst = clamp(TileIndex(origin, x + radius, tile));
const int rowLast = clamp(TileIndex(origin, x - radius, tile));
const int columnFirst = clamp(TileIndex(origin, y + radius, tile));
const int columnLast = clamp(TileIndex(origin, y - radius, tile));
auto land = std::make_unique<Land>();
for (int row = rowFirst; row <= rowLast; ++row) {
for (int column = columnFirst; column <= columnLast; ++column) {
char path[260];
std::snprintf(path, sizeof path, "World\\Maps\\%s\\%s_%d_%d.adt", dir, dir, column, row);
auto t = std::make_unique<Tile>();
t->column = static_cast<uint32_t>(column);
t->row = static_cast<uint32_t>(row);
if (!File(path).ReadAll(t->file) || !ParseHeader(*t))
continue;
const uint32_t textures = TextureCount(*t);
for (uint32_t i = 0; i < Offsets::ADT_CHUNKS; ++i) {
const uint8_t *record = CheckedRecord(*t, i, textures);
if (record != nullptr && ChunkInRadius(record, x, y, radius, side))
t->wanted.push_back(i);
}
if (t->wanted.empty())
continue;
for (uint32_t i : t->wanted) {
Chunk c;
c.memory = std::make_unique<ChunkMemory>();
c.tile = t.get();
c.index = i;
const uint32_t record = U32(McinEntry(*t, i));
const uint8_t *header = At(t->file, uint64_t{record} + Offsets::MCNK_RECORD_HEADER);
c.detailSafe = Inside(t->file,
uint64_t{record} + U32(header + Offsets::OFF_MCNK_MCSH) +
Offsets::MCNK_RECORD_HEADER,
Offsets::MCSH_SIZE);
land->chunks.push_back(std::move(c));
}
land->tiles.push_back(std::move(t));
}
}
if (land->chunks.empty())
return nullptr;
if (!MakeBuffers(*land)) {
FreeBuffers(*land);
return nullptr;
}
land->mapID = mapID;
BuildAll(*land);
MakeLiquidBuffers(*land);
AttachLiquidBuffers(*land);
MakeProps(*land, frame);
static uint32_t serials = 0;
land->serial = ++serials;
return land.release();
}
uint32_t Serial(const Land &land) { return land.serial; }
uint32_t MapOf(const Land &land) { return land.mapID; }
void Update(Land &land, void *frame, bool sounds) {
if (land.props != nullptr)
World::UpdateProps(*land.props, frame, sounds);
}
void Silence(Land &land) {
if (land.props != nullptr)
World::SilenceProps(*land.props);
}
void Free(Land *land) {
if (land == nullptr)
return;
World::FreeProps(land->props);
UnloadAll(*land);
FreeBuffers(*land);
delete land;
}
namespace {
// What a terrain pass draws under, as the world's frame has it: the camera and
// the fog end the chunk and liquid draws read (the scene's while it draws), the
// scene's volume on the frustum stack and the world transform stack pushed.
class Staged {
public:
Staged(void *frame, const float (&eye)[3], const World::ViewVolume &volume, float farClip) : eye_(eye) {
// The fog end the world's draws compare with: the scene's fog, else
// the far clip, past which nothing draws.
const bool fogged =
(Game::Read<uint32_t>(frame, Offsets::OFF_SIMPLEMODEL_FOG_FLAGS) & Offsets::SIMPLEMODEL_FOG_ARMED) != 0;
fogEnd = fogged ? Game::Read<float>(frame, Offsets::OFF_SIMPLEMODEL_FOG_FAR) : farClip;
// The view looks from the origin, so its third column is the view
// axis for a point relative to the eye.
Gx::GetView(view);
float *camera = &Game::Ref<float>(Offsets::VAR_CAMERA_POSITION);
float &worldFogEnd = Game::Ref<float>(Offsets::VAR_FOG_END);
for (int i = 0; i < 3; ++i) {
savedCamera_[i] = camera[i];
camera[i] = eye[i];
}
savedFogEnd_ = worldFogEnd;
worldFogEnd = fogEnd;
reinterpret_cast<void(__cdecl *)()>(Offsets::FUN_FRUSTUM_PUSH)();
reinterpret_cast<void(__thiscall *)(const void *)>(Offsets::FUN_FRUSTUM_STORE)(volume.bytes);
Gx::PushTransform(Offsets::GX_XFORM_WORLD);
}
~Staged() {
Gx::PopTransform(Offsets::GX_XFORM_WORLD);
reinterpret_cast<void(__cdecl *)()>(Offsets::FUN_FRUSTUM_POP)();
float *camera = &Game::Ref<float>(Offsets::VAR_CAMERA_POSITION);
for (int i = 0; i < 3; ++i)
camera[i] = savedCamera_[i];
Game::Ref<float>(Offsets::VAR_FOG_END) = savedFogEnd_;
}
Staged(const Staged &) = delete;
Staged &operator=(const Staged &) = delete;
// The depth along the view axis of a point, less `radius`.
float Depth(const float *p, float radius) const {
return (p[0] - eye_[0]) * view[2] + (p[1] - eye_[1]) * view[6] + (p[2] - eye_[2]) * view[10] - radius;
}
// The world transform at a chunk's position less the eye.
void PlaceAt(const void *chunk) const {
const float *position = Game::Ptr<float>(chunk, Offsets::OFF_CMAPCHUNK_POSITION);
Matrix world;
Identity(world);
Translate(world, {position[0] - eye_[0], position[1] - eye_[1], position[2] - eye_[2]});
Gx::SetTransform(Offsets::GX_XFORM_WORLD, world);
}
bool Culled(const float (&box)[6]) const {
return reinterpret_cast<uint32_t(__thiscall *)(const float *)>(Offsets::FUN_FRUSTUM_IS_BOX_CULLED)(box) != 0;
}
float fogEnd = 0.0f;
Matrix view;
private:
const float (&eye_)[3];
float savedCamera_[3] = {};
float savedFogEnd_ = 0.0f;
};
// A lighting record committed at the eye: the scene's lights at the sphere,
// the scene's fog, and its light (the sun's stand-in) when `lit`.
void CommitRecord(void *frame, const float (&sphere)[4], bool lit, const float (&eye)[3]) {
using Init_t = void(__thiscall *)(void *record, void *unused, const float *sphere);
using Lights_t = void(__thiscall *)(void *scene, void *record);
using Commit_t = void(__thiscall *)(void *record, const float *origin, int fog);
using Callback_t = void(__fastcall *)(void *instance, void *record, void *frame);
alignas(16) uint8_t record[Offsets::M2_LIGHTING_RECORD_SIZE] = {};
reinterpret_cast<Init_t>(Offsets::FUN_M2LIGHTING_INIT)(record, nullptr, sphere);
reinterpret_cast<Lights_t>(Offsets::FUN_M2SCENE_ADD_LIGHTS_FOR_POINT)(Model::Pane::Scene(frame), record);
if (lit)
reinterpret_cast<Callback_t>(Offsets::FUN_SIMPLEMODELFFX_LIGHT_FOG_CB)(nullptr, record, frame);
else
World::FrameFog(record, frame);
reinterpret_cast<Commit_t>(Offsets::FUN_M2LIGHTING_SET_DEVICE_LIGHTS)(record, eye, 1);
}
} // namespace
void Draw(Land &land, void *frame, const float (&eye)[3], const World::ViewVolume &volume, float farClip) {
const uint32_t worldFlags = Game::Read<uint32_t>(Offsets::VAR_WORLD_FLAGS);
if ((worldFlags & Offsets::WORLD_FLAG_CHUNKS) == 0)
return;
if (!(Settings::Current() == land.built)) {
UnloadAll(land);
BuildAll(land);
}
// The detail pools, as the world's frame prepares them (0x0068121D) before
// its passes; when they were rebuilt since, the scene's batches were made
// for the old density and are scattered again.
if (Game::Read<uint32_t>(Offsets::VAR_DETAIL_DOODAD_REBUILD) != 0)
reinterpret_cast<void(__cdecl *)()>(Offsets::FUN_PREPARE_DETAIL_DOODAD_BUFFERS)();
const uint32_t detailStamp = Game::Read<uint32_t>(Offsets::VAR_DETAIL_DOODAD_STAMP);
if (detailStamp != land.detailStamp) {
for (Chunk &c : land.chunks)
FreeDetail(c.object());
land.detailStamp = detailStamp;
}
const bool details = (worldFlags & Offsets::WORLD_FLAG_DETAIL_DOODADS) != 0 &&
Game::Read<void *>(Offsets::VAR_WORLD_MODEL_MANAGER) != nullptr;
const float horizon = Game::Read<float>(Offsets::VAR_DETAIL_DOODAD_HORIZON);
std::vector<void *> detailed; // the chunks the detail pass draws
const Staged staged(frame, eye, volume, farClip);
const float fogEnd = staged.fogEnd;
// The chunk update (0x006AFAD0) for every chunk: the sphere's view depth
// and the level, before any mesh is keyed (SelectMesh reads the
// neighbours' levels).
const bool lod = (worldFlags & Offsets::WORLD_FLAG_LOD) != 0;
const float lodDist = Game::Read<float>(Offsets::VAR_CHUNK_LOD_DIST);
const uint32_t nearLevel = Game::Read<uint32_t>(Offsets::VAR_CHUNK_MAX_LOD);
const uint32_t farLevel = Game::Read<uint32_t>(Offsets::VAR_CHUNK_FAR_LOD);
for (Chunk &c : land.chunks) {
void *o = c.object();
const float depth = staged.Depth(Game::Ptr<float>(o, Offsets::OFF_CMAPCHUNK_CENTRE),
Game::Read<float>(o, Offsets::OFF_CMAPCHUNK_RADIUS));
Game::Ref<float>(o, Offsets::OFF_CMAPCHUNK_VIEW_DEPTH) = depth;
Game::Ref<uint32_t>(o, Offsets::OFF_CMAPCHUNK_LEVEL) = lod && depth > lodDist ? farLevel : nearLevel;
}
using Mesh_t = void(__thiscall *)(void *chunk, uint32_t draw);
// The world transform at the chunk less the eye, and the chunk's lighting
// record at its sphere: the fog always, the light only nearer than the
// fog end by twice the radius (a NaN depth adds none).
const auto placeAndLight = [&](void *o) {
staged.PlaceAt(o);
const float *centre = Game::Ptr<float>(o, Offsets::OFF_CMAPCHUNK_CENTRE);
const float sphere[4] = {centre[0], centre[1], centre[2], Game::Read<float>(o, Offsets::OFF_CMAPCHUNK_RADIUS)};
const float reach = Game::Read<float>(o, Offsets::OFF_CMAPCHUNK_VIEW_DEPTH) - sphere[3];
CommitRecord(frame, sphere, reach < fogEnd, eye);
};
// The chunk pass in its own state push (DrawChunkPass 0x00684510): the
// shader path's layer draw (0x006BF690) leaves its pixel program bound,
// and only the pass's pop takes it off.
Gx::PushStates();
if ((worldFlags & Offsets::WORLD_FLAG_ANISOTROPIC) != 0) {
const float bias = Game::Read<float>(Offsets::VAR_CHUNK_MIP_BIAS_IMMEDIATE);
for (int i = 0; i < Offsets::MIP_BIAS_STAGES; ++i)
Gx::SetStateFloat(Offsets::GXRS_MIP_BIAS_STAGE0 + i, bias);
}
for (Chunk &c : land.chunks) {
void *o = c.object();
const uint8_t *header = Game::Read<const uint8_t *>(o, Offsets::OFF_CMAPCHUNK_HEADER);
if (Game::Read<uint16_t>(header, Offsets::OFF_MCNK_HOLES) == Offsets::MCNK_HOLES_ALL ||
staged.Culled(*reinterpret_cast<const float(*)[6]>(Game::Ptr<float>(o, Offsets::OFF_CMAPCHUNK_BOX))))
continue;
placeAndLight(o);
reinterpret_cast<Mesh_t>(Offsets::FUN_CMAPCHUNK_SELECT_MESH)(o, 1);
// The detail leg (0x006847C9): a chunk inside the horizon scatters its
// batch once and stays for the detail pass.
if (details && c.detailSafe && Game::Read<float>(o, Offsets::OFF_CMAPCHUNK_VIEW_DEPTH) < horizon) {
if (Game::Read<void *>(o, Offsets::OFF_CMAPCHUNK_DETAIL_BATCH) == nullptr)
reinterpret_cast<void(__thiscall *)(void *)>(Offsets::FUN_CMAPCHUNK_CREATE_DETAIL_DOODADS)(o);
if (Game::Read<void *>(o, Offsets::OFF_CMAPCHUNK_DETAIL_BATCH) != nullptr)
detailed.push_back(o);
}
}
Gx::PopStates();
// The detail pass (0x006813D0): stage 0's mip bias, then each listed
// chunk's batch at its transform and light. Each batch then gives its
// slot buffers back (the world's idle release), so none outlives a
// rebuild of the world's pools.
if (!detailed.empty()) {
Gx::PushStates();
Gx::SetStateFloat(Offsets::GXRS_MIP_BIAS_STAGE0, Game::Read<float>(Offsets::VAR_DETAIL_MIP_BIAS_IMMEDIATE));
using DrawDetail_t = void(__thiscall *)(void *chunk, float draw);
for (void *o : detailed) {
placeAndLight(o);
reinterpret_cast<DrawDetail_t>(Offsets::FUN_CMAPCHUNK_DRAW_DETAIL_DOODADS)(
o, std::numeric_limits<float>::denorm_min());
}
Gx::PopStates();
for (void *o : detailed)
reinterpret_cast<void(__thiscall *)(void *)>(Offsets::FUN_RELEASE_DETAIL_BATCH_BUFFERS)(
Game::Read<void *>(o, Offsets::OFF_CMAPCHUNK_DETAIL_BATCH));
}
}
namespace {
// The slots of one pass the scene's view shows (the layer cull 0x00683AB0):
// a slot's box is its chunk's with the slot's heights as z (0x0068DF40).
std::vector<void *> VisibleSlots(Land &land, const Staged &staged, uint32_t pass) {
std::vector<void *> out;
for (Chunk &c : land.chunks) {
void *slot = LiquidSlot(c.object(), pass);
if (slot == nullptr)
continue;
float box[6];
std::memcpy(box, Game::Ptr<float>(c.object(), Offsets::OFF_CMAPCHUNK_BOX_MIN), sizeof box);
box[2] = Game::Read<float>(slot, Offsets::OFF_CHUNKLIQUID_LOW);
box[5] = Game::Read<float>(slot, Offsets::OFF_CHUNKLIQUID_HIGH);
if (!staged.Culled(box))
out.push_back(slot);
}
return out;
}
// A pass's slots (0x00685010, 0x006851B0, 0x006855A0 per slot): with world
// flag 0x1000000, the transform at the slot's chunk, GatherSlotLights'
// record (0x0068DF70) and the slot's draw.
void DrawSlots(const std::vector<void *> &slots, uint32_t pass, const Staged &staged, void *frame,
const float (&eye)[3]) {
if ((Game::Read<uint32_t>(Offsets::VAR_WORLD_FLAGS) & Offsets::WORLD_FLAG_LIQUIDS) == 0)
return;
const float half = Game::Read<float>(Offsets::VAR_HALF);
const float radius = Game::Read<float>(Offsets::VAR_LIQUID_LIGHT_RADIUS_IMMEDIATE);
for (void *slot : slots) {
const void *chunk = Game::Read<const void *>(slot, Offsets::OFF_CHUNKLIQUID_CHUNK);
staged.PlaceAt(chunk);
const float *centre = Game::Ptr<float>(chunk, Offsets::OFF_CMAPCHUNK_CENTRE);
const float low = Game::Read<float>(slot, Offsets::OFF_CHUNKLIQUID_LOW);
const float high = Game::Read<float>(slot, Offsets::OFF_CHUNKLIQUID_HIGH);
const float sphere[4] = {centre[0], centre[1],
static_cast<float>((static_cast<double>(high) - low) * half + low), radius};
// The sun and the chunk's lights when the depth less twice the radius
// is inside the fog end (0x0068E128).
CommitRecord(frame, sphere, staged.Depth(sphere, radius) - radius < staged.fogEnd, eye);
reinterpret_cast<void(__thiscall *)(void *, uint32_t)>(Offsets::FUN_CHUNKLIQUID_DRAW)(slot, pass);
}
}
// The water passes' states (0x00685010 ocean, 0x006851B0 river): the swatch
// on stage 0, then the water program with its material, or the add combine.
// The river's leg for a third-stage program (0x00C800EC) is left out: nothing
// writes that program.
void WaterStates(uintptr_t swatch) {
Gx::SetState(Offsets::GXRS_CULL, 0);
Gx::SetState(Offsets::GXRS_TEXTURE0, Game::Read<void *>(swatch));
if (Game::Read<uint8_t>(Offsets::VAR_FANCY_WATER) == 0) {
Gx::SetState(Offsets::GXRS_PIXEL_SHADER, static_cast<const void *>(nullptr));
Gx::SetState(Offsets::GXRS_COMBINER0 + 1, Offsets::LIQUID_STAGE1_ADD);
return;
}
Gx::SetState(Offsets::GXRS_PIXEL_SHADER, Game::Read<void *>(Offsets::VAR_WATER_PROGRAM));
Gx::SetStateWord(Offsets::GXRS_SPECULAR, 0xFFFFFFFFu);
Gx::SetStateFloat(Offsets::GXRS_SHININESS, Game::Read<float>(Offsets::VAR_WATER_SHININESS));
Gx::SetState(Offsets::GXRS_BLEND, Offsets::GXRS_BLEND_ALPHA);
Gx::SetState(Offsets::GXRS_DEPTH_WRITE, 0);
}
} // namespace
void DrawMagma(Land &land, void *frame, const float (&eye)[3], const World::ViewVolume &volume, float farClip) {
if ((Game::Read<uint32_t>(Offsets::VAR_WORLD_FLAGS) & Offsets::WORLD_FLAG_CHUNKS) == 0)
return;
const Staged staged(frame, eye, volume, farClip);
const std::vector<void *> slots = VisibleSlots(land, staged, Offsets::LIQUID_PASS_MAGMA);
if (slots.empty())
return;
// DrawMagmaPass (0x006855A0): culling, lighting and blending off.
const Gx::SavedStates states;
Gx::SetState(Offsets::GXRS_CULL, 0);
Gx::SetState(Offsets::GXRS_LIGHTING, 0);
Gx::SetState(Offsets::GXRS_BLEND, 0);
DrawSlots(slots, Offsets::LIQUID_PASS_MAGMA, staged, frame, eye);
}
void DrawWater(Land &land, void *frame, const float (&eye)[3], const World::ViewVolume &volume, float farClip,
uint32_t cameraLiquid) {
if ((Game::Read<uint32_t>(Offsets::VAR_WORLD_FLAGS) & Offsets::WORLD_FLAG_CHUNKS) == 0)
return;
const Staged staged(frame, eye, volume, farClip);
const std::vector<void *> ocean = VisibleSlots(land, staged, Offsets::LIQUID_PASS_OCEAN);
const std::vector<void *> river = VisibleSlots(land, staged, Offsets::LIQUID_PASS_RIVER);
if (ocean.empty() && river.empty())
return;
// The swatches in the map's water colours at the eye: the first slot each
// pass draws refills its swatch from the light state while its fresh flag
// is clear (0x0068DE9E, 0x0068DEED); clearing them again after lets the
// world's next frame refill them in its own.
const World::MapWater water(land.mapID, cameraLiquid, eye);
uint32_t &riverFresh = Game::Ref<uint32_t>(Offsets::VAR_RIVER_SWATCH_FRESH);
uint32_t &oceanFresh = Game::Ref<uint32_t>(Offsets::VAR_OCEAN_SWATCH_FRESH);
if (water.Swapped())
riverFresh = oceanFresh = 0;
// RenderLiquid's order (0x006816D0): the ocean, then the river.
if (!ocean.empty()) {
const Gx::SavedStates states;
WaterStates(Offsets::VAR_OCEAN_SWATCH);
DrawSlots(ocean, Offsets::LIQUID_PASS_OCEAN, staged, frame, eye);
}
if (!river.empty()) {
const Gx::SavedStates states;
WaterStates(Offsets::VAR_RIVER_SWATCH);
DrawSlots(river, Offsets::LIQUID_PASS_RIVER, staged, frame, eye);
}
if (water.Swapped())
riverFresh = oceanFresh = 0;
}
// ---- collision (layer 3) ------------------------------------------------------
//
// The world's terrain legs over the scene's chunks. The world finds a chunk
// through its global area table, so its own two functions cannot see these;
// both are transcribed with the arithmetic in the client's order (x87 at
// 53 bits, float stores where it stores), and call its helpers where they
// are pure: the trace's four cell walks and its ray-triangle test. A cell
// outside the scene's chunks has no ground: the world, which loads every
// tile it needs, fails a box query there and ends a trace; the scene's
// terrain ends at its radius, past which a WMO may still stand.
namespace {
double D(float v) { return static_cast<double>(v); }
// The cell of a distance from the map's origin (0x006AA9B4, 0x0069C375):
// the product stored as a float, less a half, fistp (nearest).
int32_t Cell(float perYard, float half, float distance) {
const float product = static_cast<float>(D(perYard) * distance);
return static_cast<int32_t>(std::lrint(D(product) - half));
}
void *ChunkAt(const Land &land, int32_t row, int32_t column) {
if (row < 0 || column < 0)
return nullptr;
const auto found = land.byCell.find({static_cast<uint32_t>(row), static_cast<uint32_t>(column)});
return found != land.byCell.end() ? found->second : nullptr;
}
const float *Vertices(const void *chunk) { return Game::Ptr<float>(chunk, Offsets::OFF_CMAPCHUNK_VERTICES); }
uint16_t Holes(const void *chunk) {
return Game::Read<uint16_t>(Game::Read<const uint8_t *>(chunk, Offsets::OFF_CMAPCHUNK_HEADER),
Offsets::OFF_MCNK_HOLES);
}
// The hole bit of a cell: (column >> 1) + (row >> 1) x 4 of the table.
bool CellIsHole(const void *chunk, int32_t row, int32_t column) {
const uint32_t bit =
Game::Read<uint32_t>(Offsets::VAR_HOLE_BITS + ((column >> 1) + (row >> 1) * 4) * sizeof(uint32_t));
return (bit & Holes(chunk)) != 0;
}
// A wall facet: its axis normal, d = -n.p, and its vertices.
Contact::Facet WallFacet(const float (&n)[3], const float (&a)[3], const float (&b)[3], const float (&c)[3]) {
Contact::Facet f;
std::memcpy(f.plane.n, n, sizeof f.plane.n);
f.plane.d = static_cast<float>(-((D(n[0]) * a[0] + D(n[1]) * a[1]) + D(n[2]) * a[2]));
std::memcpy(f.v[0], a, sizeof f.v[0]);
std::memcpy(f.v[1], b, sizeof f.v[1]);
std::memcpy(f.v[2], c, sizeof f.v[2]);
return f;
}
void Add3(const float (&a)[3], const float (&b)[3], float (&out)[3]) {
for (int i = 0; i < 3; ++i)
out[i] = static_cast<float>(D(a[i]) + b[i]);
}
// AddImpassableWalls (0x006AB530): a quad of two facets on each side of the
// chunk's box the query box crosses, in the order y-min, y-max, x-min, x-max.
void AddWalls(const void *chunk, const float (&box)[6], std::vector<Contact::Facet> &out) {
const float *lo = Game::Ptr<float>(chunk, Offsets::OFF_CMAPCHUNK_BOX_MIN);
const float *hi = Game::Ptr<float>(chunk, Offsets::OFF_CMAPCHUNK_BOX_MAX);
const float up[3] = {0.0f, 0.0f, Game::Read<float>(Offsets::VAR_CHUNK_WALL_HEIGHT_IMMEDIATE)};
const auto side = [&](const float (&p)[3], const float (&e)[3], const float (&n)[3]) {
float pe[3], pu[3], peu[3];
Add3(p, e, pe);
Add3(pe, up, peu);
Add3(p, up, pu);
out.push_back(WallFacet(n, p, pe, peu));
out.push_back(WallFacet(n, p, peu, pu));
};
if (box[1] < lo[1]) {
const float p[3] = {lo[0], lo[1], lo[2]};
const float e[3] = {static_cast<float>(D(hi[0]) - lo[0]), 0.0f, 0.0f};
const float n[3] = {0.0f, -1.0f, 0.0f};
side(p, e, n);
}
if (box[4] > hi[1]) {
const float p[3] = {hi[0], hi[1], lo[2]};
const float e[3] = {static_cast<float>(D(lo[0]) - hi[0]), 0.0f, 0.0f};
const float n[3] = {0.0f, 1.0f, 0.0f};
side(p, e, n);
}
if (box[0] < lo[0]) {
const float p[3] = {lo[0], hi[1], lo[2]};
const float e[3] = {0.0f, static_cast<float>(D(lo[1]) - hi[1]), 0.0f};
const float n[3] = {-1.0f, 0.0f, 0.0f};
side(p, e, n);
}
if (box[3] > hi[0]) {
const float p[3] = {hi[0], lo[1], lo[2]};
const float e[3] = {0.0f, static_cast<float>(D(hi[1]) - lo[1]), 0.0f};
const float n[3] = {1.0f, 0.0f, 0.0f};
side(p, e, n);
}
}
// The outcode of a chunk-relative vertex against the shifted box (0x006AAF60):
// the sign bits of float(v - min + e), then of float(max - v + e).
uint32_t Outcode(const float *v, const float (&box)[6], float e) {
uint32_t code = 0;
for (int i = 0; i < 3; ++i) {
if (std::signbit(static_cast<float>((D(v[i]) - box[i]) + e)))
code |= 1u << i;
if (std::signbit(static_cast<float>((D(box[i + 3]) - v[i]) + e)))
code |= 8u << i;
}
return code;
}
// A chunk's part of a box query (GatherChunkFacets 0x006AAE2A, TestChunkBox
// 0x006AD610): the query's cell rectangle clipped to the chunk's cells, {row
// first, column first, row last, column last}, and the box less the chunk's
// position, {min, max}.
struct Window {
int32_t rect[4];
float box[6];
};
Window ChunkWindow(const void *chunk, int32_t row, int32_t column, const int32_t (&cells)[4],
const float (&box)[6]) {
const int32_t last = Offsets::CHUNK_CELLS - 1;
Window w = {{(std::max)(cells[0] - row * Offsets::CHUNK_CELLS, 0),
(std::max)(cells[1] - column * Offsets::CHUNK_CELLS, 0),
(std::min)(cells[2] - row * Offsets::CHUNK_CELLS, last),
(std::min)(cells[3] - column * Offsets::CHUNK_CELLS, last)},
{}};
const float *position = Game::Ptr<float>(chunk, Offsets::OFF_CMAPCHUNK_POSITION);
for (int i = 0; i < 3; ++i) {
const float shift = -position[i];
w.box[i] = static_cast<float>(D(box[i]) + shift);
w.box[i + 3] = static_cast<float>(D(box[i + 3]) + shift);
}
return w;
}
bool Impassable(const void *chunk) {
return (Game::Read<uint8_t>(chunk, Offsets::OFF_CMAPCHUNK_FLAGS) & Offsets::CMAPCHUNK_FLAG_IMPASSABLE) != 0;
}
// GatherChunkFacets' wall and triangle legs (0x006AAE2A..0x006AB3F1).
void GatherChunk(const void *chunk, int32_t row, int32_t column, const int32_t (&cells)[4], const float (&box)[6],
std::vector<Contact::Facet> &out) {
if (Impassable(chunk))
AddWalls(chunk, box, out);
const Window w = ChunkWindow(chunk, row, column, cells, box);
const int32_t rowFirst = w.rect[0], columnFirst = w.rect[1], rowLast = w.rect[2], columnLast = w.rect[3];
const float *position = Game::Ptr<float>(chunk, Offsets::OFF_CMAPCHUNK_POSITION);
const float e = Game::Read<float>(Offsets::VAR_GATHER_EPSILON);
const auto *probe = reinterpret_cast<const int32_t *>(static_cast<uintptr_t>(Offsets::VAR_CELL_OUTCODE_VERTICES));
const auto *triangles = reinterpret_cast<const int32_t *>(static_cast<uintptr_t>(Offsets::VAR_CELL_FACET_TRIANGLES));
const float *vertices = Vertices(chunk);
for (int32_t r = rowFirst; r <= rowLast; ++r) {
for (int32_t c = columnFirst; c <= columnLast; ++c) {
if (CellIsHole(chunk, r, c))
continue;
const float *corner = vertices + (r * Offsets::CHUNK_ROW_VERTICES + c) * 3;
uint32_t codes[Offsets::CHUNK_ROW_VERTICES + 2] = {};
for (int k = 0; k < Offsets::CELL_OUTCODE_VERTICES; ++k)
codes[probe[k]] = Outcode(corner + probe[k] * 3, w.box, e);
for (int t = 0; t < Offsets::CELL_TRIANGLES; ++t) {
const int32_t *tri = triangles + t * 3;
if ((codes[tri[0]] & codes[tri[1]] & codes[tri[2]]) != 0)
continue;
float v[3][3];
for (int k = 0; k < 3; ++k)
for (int i = 0; i < 3; ++i)
v[k][i] = static_cast<float>(D(corner[tri[k] * 3 + i]) + position[i]);
out.push_back(Contact::MakeFacet(v[0], v[1], v[2]));
}
}
}
}
} // namespace
namespace {
// The box query's cells (0x006AA92D): the distances from the origin, the
// bounds, the cells {max x, max y, min x, min y}. False outside the map.
bool BoxCells(const float (&box)[6], int32_t (&cells)[4]) {
const float origin = Game::Read<float>(Offsets::VAR_MAP_ORIGIN);
const float fromMaxX = static_cast<float>(-(D(box[3]) - origin));
const double fromMaxY = -(D(box[4]) - origin);
const float fromMinX = static_cast<float>(-(D(box[0]) - origin));
const float fromMinY = static_cast<float>(-(D(box[1]) - origin));
const float extent = Game::Read<float>(Offsets::VAR_MAP_EXTENT);
if (fromMaxY < 0.0 || fromMaxX < 0.0f || fromMinY >= extent || fromMinX >= extent)
return false;
const float perYard = Game::Read<float>(Offsets::VAR_GATHER_CELLS_PER_YARD);
const float half = Game::Read<float>(Offsets::VAR_GATHER_HALF_CELL);
cells[0] = Cell(perYard, half, fromMaxX);
cells[1] = Cell(perYard, half, static_cast<float>(fromMaxY));
cells[2] = Cell(perYard, half, fromMinX);
cells[3] = Cell(perYard, half, fromMinY);
return true;
}
} // namespace
bool QueryFacets(Land &land, const float (&box)[6], std::vector<Contact::Facet> &out) {
int32_t cells[4];
if (!BoxCells(box, cells))
return false;
// One query id for every chunk's doodads (0x006AA8BF advances it).
if (land.props != nullptr)
World::BeginPropQuery(*land.props);
bool complete = true;
for (int32_t row = cells[0] >> 3; row <= cells[2] >> 3; ++row) {
for (int32_t column = cells[1] >> 3; column <= cells[3] >> 3; ++column) {
const void *chunk = ChunkAt(land, row, column);
if (chunk == nullptr)
continue;
GatherChunk(chunk, row, column, cells, box, out);
// GatherChunkFacets' doodad leg (0x006AB496), after its triangles;
// a doodad still loading fails the chunk.
const auto found = land.propsOf.find(chunk);
if (land.props == nullptr || found == land.propsOf.end())
continue;
for (uint32_t prop : *found->second) {
if (!World::AddPropFacets(*land.props, prop, box, out)) {
complete = false;
break;
}
}
}
}
return complete;
}
bool CollectDecalBatches(const Land &land, const float (&box)[6], uint32_t mask) {
// TestTerrainBox (0x006AD4E0): the cells, the first from the unrounded
// distance (0x006AD525 multiplies before it stores).
const float origin = Game::Read<float>(Offsets::VAR_MAP_ORIGIN);
const float perYard = Game::Read<float>(Offsets::VAR_DECAL_CELLS_PER_YARD);
const float half = Game::Read<float>(Offsets::VAR_DECAL_HALF_CELL);
const float fromMaxX = static_cast<float>(-(D(box[3]) - origin));
const float fromMaxY = static_cast<float>(-(D(box[4]) - origin));
const float fromMinX = static_cast<float>(-(D(box[0]) - origin));
const double fromMinY = -(D(box[1]) - origin);
const int32_t minY = static_cast<int32_t>(std::lrint(D(static_cast<float>(D(perYard) * fromMinY)) - half));
const int32_t cells[4] = {Cell(perYard, half, fromMaxX), Cell(perYard, half, fromMaxY),
Cell(perYard, half, fromMinX), minY};
using Triangles_t = uint8_t(__fastcall *)(void *chunk, const int32_t *rect, const float *box, uint32_t unused);
using Gather_t = uint8_t(__thiscall *)(void *slot, void *unused, const float *box, const int32_t *cells);
bool any = false;
for (int32_t row = cells[0] >> 3; row <= cells[2] >> 3; ++row) {
for (int32_t column = cells[1] >> 3; column <= cells[3] >> 3; ++column) {
void *chunk = ChunkAt(land, row, column);
if (chunk == nullptr)
continue;
const Window w = ChunkWindow(chunk, row, column, cells, box);
if ((mask & Offsets::CHUNK_TRIANGLE_CLASS) != 0 &&
reinterpret_cast<Triangles_t>(Offsets::FUN_TEST_CHUNK_TRIANGLES)(chunk, w.rect, w.box, 0) != 0)
any = true;
// The liquid leg (0x006AD75E): each slot the class names into the
// same batches.
for (uint32_t i = 0; i < Offsets::CHUNK_LIQUID_SLOTS; ++i) {
void *slot = LiquidSlot(chunk, i);
if (slot == nullptr || (mask & (1u << (Offsets::LIQUID_CLASS_FIRST_BIT + i))) == 0)
continue;
uint32_t unused = 0;
if (reinterpret_cast<Gather_t>(Offsets::FUN_CHUNKLIQUID_GATHER_CELL_FACETS)(slot, &unused, w.box,
w.rect) != 0)
any = true;
}
}
}
return any;
}
uint32_t AreaAt(const Land &land, const float (&point)[3]) {
// 0x0069B126: x's distance stored, then times k and stored; y's product
// stored once; each less a half, rounded.
const float origin = Game::Read<float>(Offsets::VAR_MAP_ORIGIN);
const float perYard = Game::Read<float>(Offsets::VAR_AREA_CHUNKS_PER_YARD);
const float half = Game::Read<float>(Offsets::VAR_POINT_HALF_CELL);
const float y = static_cast<float>(-(D(point[1]) - origin) * perYard);
const int32_t column = static_cast<int32_t>(std::lrint(D(y) - half));
const int32_t row = Cell(perYard, half, static_cast<float>(-(D(point[0]) - origin)));
const void *chunk = ChunkAt(land, row, column);
return chunk != nullptr ? Game::Read<uint32_t>(chunk, Offsets::OFF_CMAPCHUNK_AREA_ID) : 0;
}
bool GroundEffect(const Land &land, const float (&point)[3], int32_t &terrainType) {
// GetGroundEffect (0x0069B1C0) over the scene's chunks.
const float origin = Game::Read<float>(Offsets::VAR_MAP_ORIGIN);
const float fromY = static_cast<float>(-(D(point[1]) - origin));
const float fromX = static_cast<float>(-(D(point[0]) - origin));
const float extent = Game::Read<float>(Offsets::VAR_MAP_EXTENT);
if (!(fromY >= 0.0f) || !(fromX >= 0.0f) || !(fromY < extent) || !(fromX < extent))
return false;
const float perYard = Game::Read<float>(Offsets::VAR_POINT_CELLS_PER_YARD);
const float half = Game::Read<float>(Offsets::VAR_POINT_HALF_CELL);
const int32_t cellColumn = Cell(perYard, half, fromY);
const int32_t cellRow = Cell(perYard, half, fromX);
const void *chunk = ChunkAt(land, cellRow >> 3, cellColumn >> 3);
if (chunk == nullptr)
return false;
const int32_t column = cellColumn & 7, row = cellRow & 7;
const uint32_t layers = Game::Read<uint32_t>(chunk, Offsets::OFF_CMAPCHUNK_LAYER_COUNT);
if (CellIsHole(chunk, row, column) || layers == 0)
return false;
const uint16_t map = Game::Read<const uint16_t *>(chunk, Offsets::OFF_CMAPCHUNK_LOW_TEXTURES)[row];
const uint16_t mask = Game::Read<uint16_t>(Offsets::VAR_LOW_TEXTURE_MASKS + column * sizeof(uint16_t));
const uint32_t shift = Game::Read<uint32_t>(Offsets::VAR_LOW_TEXTURE_SHIFTS + column * sizeof(uint32_t));
const uint32_t index = static_cast<uint32_t>(map & mask) >> shift;
// The client reads whichever layer the map names; one the chunk lacks
// would be a null record there.
if (index >= layers)
return false;
const void *layer = Game::Read<const void *>(chunk, Offsets::OFF_CMAPCHUNK_LAYERS + index * sizeof(void *));
if (layer == nullptr)
return false;
const uint16_t effect = Game::Read<uint16_t>(layer, Offsets::OFF_CHUNKLAYER_EFFECT);
if (effect == Offsets::GROUND_EFFECT_NONE)
return false;
const uint8_t *row0 = DBC::Record(Offsets::VAR_GROUNDEFFECTTEXTURE_RECORDS, Offsets::VAR_GROUNDEFFECTTEXTURE_COUNT,
effect);
if (row0 == nullptr)
return false;
terrainType = Game::Read<int32_t>(row0, Offsets::OFF_GROUNDEFFECTTEXTURE_TERRAIN_TYPE);
return true;
}
bool LiquidAt(const Land &land, const float (&point)[3], uint32_t &type, float &surface) {
// GetLiquidStatus (0x0069B709): u from y and v from x, each the negated
// distance from the origin times the cells per yard, stored as a float;
// the cells those less a half, rounded; the fractions the stored values
// less their cells, stored.
const double origin = Game::Read<float>(Offsets::VAR_MAP_ORIGIN);
const double perYard = Game::Read<float>(Offsets::VAR_POINT_CELLS_PER_YARD);
const double half = Game::Read<float>(Offsets::VAR_POINT_HALF_CELL);
const float u = static_cast<float>(-(D(point[1]) - origin) * perYard);
const float v = static_cast<float>(-(D(point[0]) - origin) * perYard);
const int32_t cellU = static_cast<int32_t>(std::lrint(D(u) - half));
const int32_t cellV = static_cast<int32_t>(std::lrint(D(v) - half));
void *chunk = ChunkAt(land, cellV >> 3, cellU >> 3);
if (chunk == nullptr)
return false;
const float fu = static_cast<float>(D(u) - cellU);
const float fv = static_cast<float>(D(v) - cellV);
const uint32_t column = static_cast<uint32_t>(cellU) & 7, row = static_cast<uint32_t>(cellV) & 7;
const double slack = Game::Read<float>(Offsets::VAR_ONE_HUNDREDTH);
for (uint32_t i = 0; i < Offsets::CHUNK_LIQUID_SLOTS; ++i) {
const void *slot = LiquidSlot(chunk, i);
if (slot == nullptr)
continue;
const uint8_t tile =
Game::Read<const uint8_t *>(slot, Offsets::OFF_CHUNKLIQUID_TILES)[row * Offsets::LIQUID_TILE_ROW + column];
if ((tile & Offsets::LIQUID_TILE_TYPE_MASK) == Offsets::LIQUID_TILE_NONE)
continue;
const uint32_t kind = tile & Offsets::LIQUID_TYPE_MASK;
// The ocean is at height 0: the point is in it under the slack.
if (kind == Offsets::LIQUID_OCEAN) {
if (D(point[2]) < slack) {
type = kind;
surface = 0.0f;
return true;
}
continue;
}
// The MCLQ heights bilinear along u (fu), then v (fv), at 53 bits.
const auto *records = Game::Read<const uint8_t *>(slot, Offsets::OFF_CHUNKLIQUID_MCLQ_VERTICES);
const uint32_t first = row * Offsets::LIQUID_VERTEX_ROW + column;
const auto h = [&](uint32_t k) {
return D(Game::Read<float>(records, k * Offsets::MCLQ_VERTEX_SIZE + Offsets::OFF_MCLQ_VERTEX_HEIGHT));
};
const uint32_t next = first + Offsets::LIQUID_VERTEX_ROW;
const double a = (h(first + 1) - h(first)) * fu + h(first);
const double b = (h(next + 1) - h(next)) * fu + h(next);
const float height = static_cast<float>((b - a) * fv + a);
if (D(height) + slack > D(point[2])) {
type = kind;
surface = height;
return true;
}
}
return false;
}
bool QueryLiquidFacets(const Land &land, const float (&box)[6], std::vector<Contact::Facet> &out) {
using Gather_t = uint8_t(__thiscall *)(void *slot, void *unused, const float *box, const int32_t *cells);
int32_t cells[4];
if (!BoxCells(box, cells))
return false;
for (int32_t row = cells[0] >> 3; row <= cells[2] >> 3; ++row) {
for (int32_t column = cells[1] >> 3; column <= cells[3] >> 3; ++column) {
void *chunk = ChunkAt(land, row, column);
if (chunk == nullptr)
continue;
// The walls ignore the class (0x006AAE09).
if (Impassable(chunk))
AddWalls(chunk, box, out);
// The liquid leg (0x006AB3F7): each slot the class names, through
// GatherLiquidFacets (0x006AB4E0).
const Window w = ChunkWindow(chunk, row, column, cells, box);
for (uint32_t i = 0; i < Offsets::CHUNK_LIQUID_SLOTS; ++i) {
void *slot = LiquidSlot(chunk, i);
if (slot == nullptr ||
(Offsets::WORLD_CLASS_SWIM_LIQUID & (1u << (Offsets::LIQUID_CLASS_FIRST_BIT + i))) == 0)
continue;
World::ResetFacetBatches();
uint32_t unused = 0;
if (reinterpret_cast<Gather_t>(Offsets::FUN_CHUNKLIQUID_GATHER_CELL_FACETS)(slot, &unused, w.box,
w.rect) != 0)
World::EmitFacetBatches(out);
}
}
}
return true;
}
namespace {
struct Ray {
float origin[3];
float direction[3];
};
// TraceTerrainSegment's walk (0x0069C320..0x0069C4B8): the ends to cells,
// then the engine's own walks fill its cell list.
void ListCells(const float (&start)[3], const float (&end)[3]) {
const float origin = Game::Read<float>(Offsets::VAR_MAP_ORIGIN);
const float startY = static_cast<float>(-(D(start[1]) - origin));
const float startX = static_cast<float>(-(D(start[0]) - origin));
const float endY = static_cast<float>(-(D(end[1]) - origin));
const float endX = static_cast<float>(-(D(end[0]) - origin));
const float dy = static_cast<float>(D(endY) - startY);
const float dx = static_cast<float>(D(endX) - startX);
const float perYard = Game::Read<float>(Offsets::VAR_TRACE_CELLS_PER_YARD);
const float half = Game::Read<float>(Offsets::VAR_TRACE_HALF_CELL);
int32_t cells[4] = {Cell(perYard, half, startX), Cell(perYard, half, startY), Cell(perYard, half, endX),
Cell(perYard, half, endY)};
Game::Ref<uint32_t>(Offsets::VAR_TRACE_CELL_COUNT) = 0;
const float epsilon = Game::Read<float>(Offsets::VAR_TWO_POW_MINUS_22);
const float spanY = std::fabs(dy);
const float spanX = std::fabs(dx);
using Run_t = void(__thiscall *)(int32_t *cells);
using Line_t = void(__fastcall *)(float *start, const float *end, int32_t *cells);
if (spanY < epsilon || cells[1] == cells[3]) {
reinterpret_cast<Run_t>(Offsets::FUN_LIST_CELL_COLUMN_RUN)(cells);
} else if (spanX < epsilon || cells[0] == cells[2]) {
reinterpret_cast<Run_t>(Offsets::FUN_LIST_CELL_ROW_RUN)(cells);
} else {
float from[3] = {startY, startX, 0.0f};
const float to[3] = {endY, endX, 0.0f};
reinterpret_cast<Line_t>(spanY > spanX ? Offsets::FUN_LIST_CELLS_ALONG_ROWS
: Offsets::FUN_LIST_CELLS_ALONG_COLUMNS)(from, to, cells);
}
}
} // namespace
bool Trace(const Land &land, const float (&start)[3], const float (&end)[3], float &fraction, float (&normal)[3]) {
ListCells(start, end);
// TraceListedCells (0x0069C920), its terrain triangles.
const uint32_t *list = Game::Read<const uint32_t *>(Offsets::VAR_TRACE_CELLS);
uint32_t left = Game::Read<uint32_t>(Offsets::VAR_TRACE_CELL_COUNT);
if (list == nullptr || left == 0)
return false;
const double dx = D(end[0]) - start[0], dy = D(end[1]) - start[1], dz = D(end[2]) - start[2];
const float dzf = static_cast<float>(dz);
const float k = static_cast<float>(1.0 / std::sqrt((dz * dzf + dy * dy) + dx * dx));
Ray ray{{start[0], start[1], start[2]},
{static_cast<float>(dx * k), static_cast<float>(dy * k), static_cast<float>(D(dzf) * k)}};
float best = fraction;
const void *hitChunk = nullptr;
uint32_t hitIndices[3] = {};
const void *chunk = nullptr;
// The walk's chunk keys start from the first pair crossed, as 0x0069C932
// loads them.
uint32_t seenA = list[1] & Offsets::TRACE_CELL_LIMIT;
uint32_t seenB = list[0] & Offsets::TRACE_CELL_LIMIT;
const auto *pairs = reinterpret_cast<const int32_t *>(static_cast<uintptr_t>(Offsets::VAR_CELL_TRACE_TRIANGLES));
const float slack = Game::Read<float>(Offsets::VAR_TRACE_SLACK_IMMEDIATE);
using Intersect_t = uint8_t(__fastcall *)(const Ray *ray, const float *vertices, const uint32_t *indices,
float *t, float *uv, float slack);
for (; left != 0; list += 2) {
const uint32_t a = list[0], b = list[1]; // the y cell, then the x cell
left -= 2;
if (a > Offsets::TRACE_CELL_LIMIT || b > Offsets::TRACE_CELL_LIMIT)
break;
if ((a & Offsets::TRACE_CELL_CHUNK_BITS) != seenA || (b & Offsets::TRACE_CELL_CHUNK_BITS) != seenB) {
chunk = ChunkAt(land, static_cast<int32_t>(b >> 3), static_cast<int32_t>(a >> 3));
if (chunk == nullptr)
break;
seenA = a & Offsets::TRACE_CELL_CHUNK_BITS;
seenB = b & Offsets::TRACE_CELL_CHUNK_BITS;
const float *position = Game::Ptr<float>(chunk, Offsets::OFF_CMAPCHUNK_POSITION);
for (int i = 0; i < 3; ++i)
ray.origin[i] = static_cast<float>(D(start[i]) - position[i]);
}
if (chunk == nullptr)
break;
const int32_t column = static_cast<int32_t>(a & 7), row = static_cast<int32_t>(b & 7);
if (CellIsHole(chunk, row, column))
continue;
const uint32_t base = static_cast<uint32_t>(row * Offsets::CHUNK_ROW_VERTICES + column);
for (int t = 0; t < Offsets::CELL_TRIANGLES; ++t) {
const uint32_t indices[3] = {base + Offsets::CELL_CENTRE_VERTEX, base + pairs[t * 2 + 1],
base + pairs[t * 2]};
float raw = 0.0f;
if (reinterpret_cast<Intersect_t>(Offsets::FUN_INTERSECT_RAY_TRIANGLE)(&ray, Vertices(chunk), indices,
&raw, nullptr, slack) == 0)
continue;
const double scaled = D(raw) * k;
if (scaled < best && !(scaled < 0.0)) {
best = static_cast<float>(scaled);
hitChunk = chunk;
std::memcpy(hitIndices, indices, sizeof hitIndices);
}
}
}
if (!(best < fraction) || hitChunk == nullptr)
return false;
fraction = best;
// The hit triangle's plane (PlaneFromTriangle, as the terrain first hit
// 0x006AA600 records it), turned to face the start.
const float *v = Vertices(hitChunk);
Contact::Plane plane;
using Plane_t = void(__fastcall *)(Contact::Plane *, int, const float *, const float *, const float *);
reinterpret_cast<Plane_t>(Offsets::FUN_PLANE_FROM_TRIANGLE)(&plane, 0, v + hitIndices[0] * 3,
v + hitIndices[1] * 3, v + hitIndices[2] * 3);
const double facing = (D(plane.n[0]) * dx + D(plane.n[1]) * dy) + D(plane.n[2]) * dz;
for (int i = 0; i < 3; ++i)
normal[i] = facing > 0.0 ? -plane.n[i] : plane.n[i];
return true;
}
} // namespace Model::Scene::Terrain