Files
ClassicAPI/src/model/SceneMover.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

2430 lines
92 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/>.
// The client's mover for a scene body (see SceneMover.h), one client function
// per function here. As in SceneContact.cpp, every sum is a double and every
// store a float cast, in the operand order of the disassembly. The sources
// are benilla-re's systems/collision docs (instruction-level notes) and its
// crates/collision transcription; where they differ, the binary was read and
// wins: the fall-away lookahead starts a real fall and sweeps (+0x68, +0x6C,
// -D) (0x00635FF7, 0x00636014), and the airborne push carries over to later
// steps (airborne.md, Stall rules).
//
// The layers, top down: the tick (0x00616800, 0x00616620) cuts the frame
// into a window, the integrator (0x00616DE0, 0x00616CB0) turns the time
// since the anchors into a displacement, the substep loop (0x00634040)
// gathers the facets around each substep (0x00633840) and runs the walk
// (0x006367B0), fall (0x00635B00) or swimmer's free-move (0x00634640)
// resolver, and those sweep the body's volume (SceneContact.h) and deflect,
// step and settle. After a tick the unit side decides whether the body swims
// (UpdateSwimState).
#include "model/SceneMover.h"
#include "Game.h"
#include "Offsets.h"
#include "model/ScenePrecision.h"
#include <cfloat>
#include <cmath>
#include <cstring>
namespace Model::Scene::Mover {
namespace {
using Game::Live;
using Precision::Bits;
using Precision::D;
const Live<float> kMsToSec{Offsets::VAR_ONE_THOUSANDTH}; // also the 0.001 nudge
const Live<float> kThousand{Offsets::VAR_ONE_THOUSAND};
const Live<float> kHalf{Offsets::VAR_HALF};
const Live<float> kOne{Offsets::VAR_ONE};
const Live<float> kTiny{Offsets::VAR_TWO_POW_MINUS_20};
// The speed setters' test (fcomp [0x008029D4] at 0x007C7041).
const Live<float> kSpeedTiny{Offsets::VAR_TWO_POW_MINUS_22};
const Live<float> kParallel{Offsets::VAR_TWO_POW_MINUS_22};
const Live<float> kSkin{Offsets::VAR_MOVER_SKIN}; // 1/720
const Live<float> kThirtySixth{Offsets::VAR_ONE_THIRTY_SIXTH};
const Live<float> kNinth{Offsets::VAR_MOVER_NINTH};
const Live<float> kDownBias{Offsets::VAR_ONE_HUNDREDTH};
const Live<float> kStall{Offsets::VAR_MOVER_STALL};
const Live<float> kStallReach{Offsets::VAR_MOVER_STALL_REACH};
const Live<float> kTenth{Offsets::VAR_ONE_TENTH};
const Live<float> kCos10{Offsets::VAR_MOVER_COS_10};
const Live<float> kCos50{Offsets::VAR_MOVER_FLOOR_COS}; // the floor limit
const Live<float> kTan50{Offsets::VAR_MOVER_FLOOR_TAN};
const Live<float> kNegTan50{Offsets::VAR_MOVER_MINUS_FLOOR_TAN};
const Live<float> kSqrt2{Offsets::VAR_MOVER_SQRT_2};
const float kRootHalf = Bits(0x3F3504F3); // √½, the diagonal
const Live<float> kWaist{Offsets::VAR_MOVER_WAIST}; // tan 61.6°
const Live<float> kBevelZ{Offsets::VAR_MOVER_BEVEL_Z}; // the bevels' normal z
const Live<float> kBisect{Offsets::VAR_MOVER_BISECT}; // the two-bevel normalizer
const Live<float> kG{Offsets::VAR_MOVER_GRAVITY};
const Live<float> kHalfG{Offsets::VAR_MOVER_HALF_GRAVITY};
const Live<float> kTwoG{Offsets::VAR_MOVER_TWO_GRAVITY};
const Live<float> kInvG{Offsets::VAR_MOVER_INV_GRAVITY};
const Live<float> kNegInvG{Offsets::VAR_MOVER_MINUS_INV_GRAVITY};
const Live<float> kTwoOverG{Offsets::VAR_MOVER_TWO_OVER_GRAVITY};
const Live<float> kTerminal{Offsets::VAR_MOVER_TERMINAL_SPEED}; // the fall's cap
const Live<float> kTerminalSlow{Offsets::VAR_MOVER_TERMINAL_SPEED_SLOW}; // the cap under 0x20000000
const float kJumpSpeed = Bits(0xC0FE93D8); // the jump's launch, an immediate (0x007C626F)
const float kSwimJumpSpeed = Bits(0xC1118C48);
const Live<float> kTwoPi{Offsets::VAR_MOVER_TWO_PI};
const Live<float> kHalfPi{Offsets::VAR_HALF_PI}; // the pitch clamp
const Live<float> kNegHalfPi{Offsets::VAR_MINUS_HALF_PI};
const Live<float> kThreeQuarters{Offsets::VAR_THREE_QUARTERS};
const Live<float> kTwoFifths{Offsets::VAR_TWO_FIFTHS};
constexpr float kFltMax = FLT_MAX;
constexpr float kNegFltMax = -FLT_MAX;
const Live<float> kSnapSq{Offsets::VAR_MOVER_SNAP_DISTANCE_SQ}; // 3 yards squared
const Live<float> kWarpSpeedSq{Offsets::VAR_MOVER_WARP_SPEED_SQ}; // 60 yards a second squared
const Live<float> kReachedSq{Offsets::VAR_MOVER_PATH_POINT_SQ}; // one path point
const Live<float> kSixth{Offsets::VAR_ONE_SIXTH}; // the shortest path
const Live<float> kFour{Offsets::VAR_FOUR}; // the path speed's cap over the run speed
const float kCellCentre = Bits(0x46855555); // 17066.666, an immediate (0x00616BF0)
const Live<float> kCellSpan{Offsets::VAR_MOVER_CELL_SPAN};
const float kDefaultRadius = Bits(0x3EAAAAAB);
const float kDefaultHeight = Bits(0x4001C71C);
// The speeds a server's create block gives a character (the block's wire
// +0x50 to +0x64, copied by 0x00619320): vmangos' baseMoveSpeed.
constexpr float kWalkSpeed = 2.5f;
constexpr float kRunSpeed = 7.0f;
constexpr float kRunBackSpeed = 4.5f;
constexpr float kSwimSpeed = 4.722222f;
constexpr float kSwimBackSpeed = 2.5f;
constexpr float kTurnRate = 3.141594f;
constexpr uint32_t kMaxWindowMs = 250; // 0x00616642
// A bound on the substep passes, which the client does not have: each of
// its passes consumes time or changes the flags, so it never loops for
// long; this keeps a fault in the port from hanging the game.
constexpr int kMaxPasses = 64;
// __ftol after the half added away from zero (0x006359AE, 0x00635FAF).
// A NaN or out-of-range value converts to 0x8000000000000000, whose low
// dword is 0.
int32_t Round(double x) {
const double biased = x > 0.0 ? x + kHalf : x - kHalf;
if (!(biased > -9.2e18 && biased < 9.2e18))
return 0;
return static_cast<int32_t>(static_cast<int64_t>(biased));
}
// DotVector3, unrounded.
double Dot(const float (&a)[3], const float (&b)[3]) {
using Fn = double(__fastcall *)(const float *, const float *);
return reinterpret_cast<Fn>(Offsets::FUN_DOT_VECTOR3)(a, b);
}
using Contact::Facet;
using Contact::Plane;
// One sweep (0x00632BA0) of the body's volume from where it stands, against
// the solid list or with `liquid` the liquid list.
struct Hit {
bool ok;
float distance;
uint32_t index;
uint32_t count;
Plane faces[9];
};
Hit Sweep(Body &b, const float (&dir)[3], float length, bool liquid = false) {
Contact::Contact c{};
Hit h{};
h.ok = Contact::Sweep(b.facets, liquid, (b.flags & FLAG_SWIMMING) != 0, b.position, b.radius, b.height, dir,
length, c);
h.distance = c.distance;
h.index = c.hit;
h.count = c.count;
std::memcpy(h.faces, c.faces, sizeof h.faces);
return h;
}
uint32_t FacetCount(const Body &b) { return static_cast<uint32_t>(b.facets.solid.size()); }
// ---- kinematics (0x007C4850 to 0x007C5CD0) ------------------------------------------
// Spline flags (+0x18).
constexpr uint32_t kSplineArrived = 0x1; // set by the follower at the duration
constexpr uint32_t kSplineDone = 0x4; // CompleteSpline (0x00619DEF)
constexpr uint32_t kSplineRun = 0x100; // the run mode (0x007C6AC2)
constexpr uint32_t kSplineFinalAngle = 0x40000; // SetSplineFacingAngle (0x006188A0)
constexpr uint32_t kSplineHeld = 0x80000000; // the follower writes nothing (0x007C54BA)
bool Live(const Body &b) { return b.spline && !(b.spline->flags & kSplineDone); }
// The current speed (0x007C4C90). A live spline's is its length over its
// duration (0x007C4CAA: fidiv by the signed duration, times 1000).
float SelectSpeed(const Body &b, bool walk) {
if ((b.flags & 0xF) == 0)
return 0.0f;
if (Live(b))
return static_cast<float>(D(b.spline->length) / static_cast<int32_t>(b.spline->duration) * kThousand);
if (b.flags & FLAG_SWIMMING) {
if (b.flags & FLAG_BACKWARD)
return b.swimBackSpeed <= b.swimSpeed ? b.swimBackSpeed : b.swimSpeed;
return b.swimSpeed;
}
if ((b.flags & 0x100) || walk)
return b.runSpeed > b.walkSpeed ? b.walkSpeed : b.runSpeed;
if (b.flags & FLAG_BACKWARD)
return b.runBackSpeed <= b.runSpeed ? b.runBackSpeed : b.runSpeed;
return b.runSpeed;
}
// 0x007C5C20.
void RefreshSpeed(Body &b, bool force) {
if ((b.flags & FLAG_FALLING) && !force)
return;
b.speed = SelectSpeed(b, force);
}
// The turn rate (0x007C5C50), unrounded.
double TurnRate(const Body &b) {
double rate = 0.0;
if (b.flags & FLAG_TURN_LEFT)
rate = b.turnRate;
else if (b.flags & FLAG_TURN_RIGHT)
rate = -D(b.turnRate);
if (b.flags & 0x200F)
rate *= 0.75;
return rate;
}
// The pitch rate (0x007C5C90), unrounded: up for 0x40, down for 0x80.
double PitchRate(const Body &b) {
double rate = 0.0;
if (b.flags & FLAG_PITCH_UP)
rate = b.turnRate;
else if (b.flags & FLAG_PITCH_DOWN)
rate = -D(b.turnRate);
if (b.flags & 0x200F)
rate *= kThreeQuarters;
return rate;
}
// The heading basis (0x007C5880).
void Basis(Body &b) {
const double f = b.anchorFacing;
b.heading[0] = static_cast<float>(std::cos(f));
b.heading[1] = static_cast<float>(std::sin(f));
const float p = b.anchorPitch;
if ((b.flags & FLAG_SWIMMING) && !(std::fabs(p) < kTiny)) {
b.pitchBasis[0] = static_cast<float>(std::cos(D(p)));
b.pitchBasis[1] = static_cast<float>(std::sin(D(p)));
b.direction[0] = static_cast<float>(D(b.heading[0]) * b.pitchBasis[0]);
b.direction[1] = static_cast<float>(D(b.heading[1]) * b.pitchBasis[0]);
b.direction[2] = b.pitchBasis[1];
} else {
b.direction[0] = b.heading[0];
b.direction[1] = b.heading[1];
b.direction[2] = 0.0f;
b.pitchBasis[0] = 1.0f;
b.pitchBasis[1] = 0.0f;
}
}
// The direction from the held bits (0x007C5A20).
void Direction(Body &b, bool force) {
if ((b.flags & FLAG_FALLING) && !force)
return;
Basis(b);
const float hx = b.heading[0], hy = b.heading[1];
float side[2] = {0.0f, 0.0f};
if (b.flags & FLAG_STRAFE_LEFT) {
side[0] = -hy;
side[1] = hx;
} else if (b.flags & FLAG_STRAFE_RIGHT) {
side[0] = hy;
side[1] = -hx;
}
if ((b.flags & 0x3) && (b.flags & 0xC)) {
const float k = (b.flags & FLAG_BACKWARD) ? -1.0f : 1.0f;
const float d[3] = {b.direction[0] * k, b.direction[1] * k, b.direction[2] * k};
const float h[2] = {hx * k, hy * k};
const float sum[5] = {static_cast<float>(D(side[0]) + d[0]), static_cast<float>(D(d[1]) + side[1]), d[2],
static_cast<float>(D(h[0]) + side[0]), static_cast<float>(D(h[1]) + side[1])};
b.direction[0] = static_cast<float>(D(sum[0]) * kRootHalf);
b.direction[1] = static_cast<float>(D(sum[1]) * kRootHalf);
b.direction[2] = static_cast<float>(D(sum[2]) * kRootHalf);
b.heading[0] = static_cast<float>(D(sum[3]) * kRootHalf);
b.heading[1] = static_cast<float>(D(sum[4]) * kRootHalf);
} else if (b.flags & 0x3) {
if (b.flags & FLAG_BACKWARD) {
for (float &c : b.direction)
c = -c;
b.heading[0] = -hx;
b.heading[1] = -hy;
}
} else if (b.flags & 0xC) {
b.direction[0] = side[0];
b.direction[1] = side[1];
b.direction[2] = 0.0f;
b.heading[0] = side[0];
b.heading[1] = side[1];
}
}
// CMovement::UpdateAnchors (0x007C5CD0).
void Anchor(Body &b, bool force) {
std::memcpy(b.anchor, b.position, sizeof b.anchor);
b.anchorFacing = b.facing;
b.anchorPitch = b.pitch;
b.elapsed = 0;
Direction(b, force);
}
// The facing at t seconds (0x007C4F30).
void IntegrateFacing(Body &b, float t) {
const float omega = static_cast<float>(TurnRate(b));
const double r = std::fmod(D(omega) * t + b.anchorFacing, D(kTwoPi));
b.facing = static_cast<float>(r < 0.0 ? r + kTwoPi : r);
}
// The pitch at t seconds (0x007C4F80), held to +-pi/2; NaN holds at pi/2.
void IntegratePitch(Body &b, float t) {
const double theta = PitchRate(b) * t + b.anchorPitch;
if (theta < kNegHalfPi)
b.pitch = kNegHalfPi;
else if (theta < kHalfPi)
b.pitch = static_cast<float>(theta);
else
b.pitch = kHalfPi;
}
// The pitch's clamp in the arcs (0x007C51B1, 0x007C5024): with theta the
// pitch at t and `inverse` the float 1/p, past +-pi/2 the time spent beyond
// it (tau), with the swept angle `delta` cut at the clamp; else 0, `delta`
// as given.
float ClampTime(const Body &b, double theta, float inverse, float &delta) {
if (theta > kHalfPi) {
delta = static_cast<float>(D(kHalfPi) - b.anchorPitch);
return static_cast<float>((theta - kHalfPi) * inverse);
}
if (theta < kNegHalfPi) {
delta = static_cast<float>(D(kNegHalfPi) - b.anchorPitch);
return static_cast<float>((theta + kHalfPi) * inverse);
}
return 0.0f;
}
// The arcs' last step (0x007C5265, 0x007C5127): past the clamp the body
// climbs or dives straight for the time tau beyond it.
float ClampClimb(const Body &b, double z, float tau, float delta, float speed) {
if (std::fabs(tau) < kTiny)
return static_cast<float>(z);
const double climb = D(tau) * speed;
if (std::fabs((D(delta) + b.anchorPitch) - kHalfPi) < kTiny)
return static_cast<float>(climb + z);
return static_cast<float>(z - climb);
}
// The straight line (0x007C4ED0).
void Straight(const Body &b, float t, float (&out)[3]) {
const double s = b.speed;
out[0] = static_cast<float>((D(t) * b.direction[0]) * s);
out[1] = static_cast<float>((D(t) * b.direction[1]) * s);
out[2] = static_cast<float>(D(static_cast<float>(D(t) * b.direction[2])) * s);
}
// The turn arc (0x007C52C0).
void TurnArc(const Body &b, float t, float (&out)[3]) {
const double s = b.speed;
const double omega = TurnRate(b);
const float radius = static_cast<float>(s / omega);
const float phi = static_cast<float>(omega * t);
const float sinPhi = static_cast<float>(std::sin(D(phi)));
const float cosPhi = static_cast<float>(std::cos(D(phi)));
const double a = D(sinPhi) * radius;
const double c = D(radius) - D(cosPhi) * radius;
const double hx = b.heading[0], hy = b.heading[1];
out[0] = static_cast<float>((a * hx - c * hy) * b.pitchBasis[0]);
out[1] = static_cast<float>((c * hx + a * hy) * b.pitchBasis[0]);
out[2] = static_cast<float>((s * b.pitchBasis[1]) * t);
}
// The pitch arc (0x007C5180): on along the heading while the pitch turns,
// a circle in the vertical plane.
void PitchArc(const Body &b, float t, float (&out)[3]) {
const float s = b.speed;
const double p = PitchRate(b);
const double inverse = D(kOne) / p;
const float rv = static_cast<float>(inverse * s);
const double pt = p * t;
float delta = static_cast<float>(pt);
const float tau = ClampTime(b, pt + b.anchorPitch, static_cast<float>(inverse), delta);
const float sinD = static_cast<float>(std::sin(D(delta)));
const float cosD = static_cast<float>(std::cos(D(delta)));
const double h = D(sinD) * rv;
const double v = D(rv) - D(cosD) * rv;
out[0] = static_cast<float>((h * b.pitchBasis[0] - v * b.pitchBasis[1]) * b.heading[0]);
out[1] = static_cast<float>((h * b.pitchBasis[0] - v * b.pitchBasis[1]) * b.heading[1]);
const double z = v * b.pitchBasis[0] + h * b.pitchBasis[1];
out[2] = ClampClimb(b, z, tau, delta, s);
}
// The turn and pitch arc (0x007C4FD0): both turning at once.
void TurnPitchArc(const Body &b, float t, float (&out)[3]) {
const float s = b.speed;
const float omega = static_cast<float>(TurnRate(b));
const double p = PitchRate(b);
const float rh = static_cast<float>(D(s) / omega);
const double inverse = D(kOne) / p;
const float rv = static_cast<float>(inverse * s);
const double pt = p * t;
float delta = static_cast<float>(pt);
const float tau = ClampTime(b, pt + b.anchorPitch, static_cast<float>(inverse), delta);
const float phi = static_cast<float>((D(t) - tau) * omega);
const float sinP = static_cast<float>(std::sin(D(phi)));
const float cosP = static_cast<float>(std::cos(D(phi)));
const float sinD = static_cast<float>(std::sin(D(delta)));
const float cosD = static_cast<float>(std::cos(D(delta)));
const double a = (D(sinD) * rv + D(sinP) * rh) * kHalf;
const double across = D(cosP) * rh - rh;
const float c = static_cast<float>(D(rv) - D(cosD) * rv);
const float x = static_cast<float>(a * b.heading[0] - across * b.heading[1]);
const double y = across * b.heading[0] + a * b.heading[1];
out[0] = static_cast<float>(D(x) * b.pitchBasis[0] - D(c) * b.pitchBasis[1]);
out[1] = static_cast<float>(y);
const double z = D(c) * b.pitchBasis[0] + D(x) * b.pitchBasis[1];
out[2] = ClampClimb(b, z, tau, delta, s);
}
// The displacement over `ms` since the anchors (0x007C5360).
bool Displacement(Body &b, uint32_t ms, float (&out)[3]) {
if (ms == 0)
return false;
const float t = static_cast<float>(D(static_cast<float>(ms)) * kMsToSec);
int mode = 0;
if (b.flags & 0x30) {
IntegrateFacing(b, t);
if (!(b.flags & FLAG_FALLING))
mode |= 2;
}
if (b.flags & 0xC0) {
IntegratePitch(b, t);
mode |= 8;
}
if (b.flags & 0x3)
mode |= 1;
if (b.flags & 0xC)
mode |= 4;
switch (mode) {
case 0:
return true;
case 1:
case 4:
case 5:
case 12:
Straight(b, t, out);
return true;
case 3:
case 6:
case 7:
case 14:
TurnArc(b, t, out);
return true;
case 9:
case 13:
PitchArc(b, t, out);
return true;
case 11:
case 15:
TurnPitchArc(b, t, out);
return true;
default: // 2, 8, 10: turning or pitching in place
return false;
}
}
// ---- the fall arc (0x007C5D20 to 0x007C6290) -----------------------------------------
double Terminal(const Body &b) { return (b.flags & 0x20000000) ? kTerminalSlow : kTerminal; }
// The fall speed's start, capped (0x007C5D40).
double CappedFallSpeed(const Body &b) {
const double t = Terminal(b), v = b.fallSpeed;
return v > t ? t : v;
}
// GetFallDistance (0x007C5E70).
double FallDistance(const Body &b, float t) {
const double cap = Terminal(b), v = CappedFallSpeed(b), dt = t;
const double end = dt * kG + v;
if (end > cap || std::isnan(end)) {
const double tT = (cap - v) * kInvG;
return (v + D(kHalfG) * tT) * tT + (dt - tT) * cap;
}
return (dt * kHalfG + v) * dt;
}
double FallDistanceMs(const Body &b, uint32_t ms) {
return FallDistance(b, static_cast<float>(D(static_cast<float>(ms)) * kMsToSec));
}
// The further drop from the current z to the arc at `ms` (0x007C6140,
// 0x007C6110).
double FallDrop(const Body &b, uint32_t ms) {
const double d = FallDistanceMs(b, ms);
if (!(b.flags & FLAG_FALLING))
return d;
return d + (D(b.position[2]) - b.fallStartZ);
}
// GetFallTime (0x007C5F50): the arc time at which the drop reaches `d`;
// with `first`, the earlier root.
double FallTime(const Body &b, float d, bool first) {
const double cap = Terminal(b);
float v = b.fallSpeed;
if (!(D(v) <= cap) && !std::isnan(v))
v = static_cast<float>(cap);
if (std::fabs(v) < kParallel) {
const double tT = D(kInvG) * cap;
const double dT = (tT * cap) * 0.5;
if (!(d < dT))
return (D(d) - dT) / cap + tT;
if (!(d > 0.0f))
return 0.0;
return std::sqrt(D(d) * kTwoOverG);
}
const double s = D(v) * v + D(d) * kTwoG;
const double root = s > 0.0 ? std::sqrt(s) : 0.0;
const float early = static_cast<float>((-D(v) - root) * kInvG);
double late = (root - v) * kInvG;
const float tT = static_cast<float>((cap - v) * kInvG);
if (late > tT)
late = (D(d) - (D(tT) * kHalfG + v) * tT) / cap + tT;
if (!first)
return late;
return early < 0.0f ? 0.0 : D(early);
}
// GetApexTime (0x007C5DA0).
double ApexTime(const Body &b) { return (b.flags & FLAG_FALLING) ? D(b.fallSpeed) * kNegInvG : 0.0; }
// Starting a fall (0x007C61F0).
void StartFall(Body &b, float v) {
Anchor(b, false);
b.flags = (b.flags & 0xFBDFFFFF) | FLAG_FALLING;
b.fallTime = 0;
b.fallStartZ = b.position[2];
b.fallSpeed = v;
}
// Landing (0x007C6290).
void Land(Body &b) {
if (!(b.flags & FLAG_FALLING))
return;
b.flags &= 0xFFFF9FFF;
Anchor(b, false);
RefreshSpeed(b, false);
}
// CMovement::RestartFallAtRest (0x00633220): a head strike.
void RestartFallAtRest(Body &b) {
b.fallSpeed = 0.0f;
b.fallTime = 0;
b.fallStartZ = b.position[2];
b.flags |= FLAG_FALLING_FAR;
}
// CMovement::LatchFallingFar (0x00633240).
void LatchFallingFar(Body &b) {
if (b.flags & 0x20004000)
return;
if ((b.flags & FLAG_FALLING) && !(b.fallSpeed == 0.0f)) {
if (!(D(b.fallStartZ) - kNinth < b.position[2]))
b.flags |= FLAG_FALLING_FAR;
return;
}
if (b.fallTime >= 500)
b.flags |= FLAG_FALLING_FAR;
}
// ---- the commands (0x007C6AE0 to 0x007C6DD0) ---------------------------------------
void StopMoveNow(Body &b) { // 0x007C6C00
b.flags &= 0xFFFF7FFC;
RefreshSpeed(b, false);
Anchor(b, false);
}
void StopStrafeNow(Body &b) { // 0x007C6CE0
b.flags &= 0xFFFEFFF3;
RefreshSpeed(b, false);
Anchor(b, false);
}
// The deferred direction latches (0x0061AB20), applied at a landing.
uint32_t ApplyLatches(Body &b) {
const uint32_t entry = b.flags;
if (b.flags & FLAG_STOP_ON_LAND)
StopMoveNow(b);
if (b.flags & 0x10000)
StopStrafeNow(b);
if (b.flags & 0x60000)
StartMove(b, (b.flags & 0x20000) != 0);
if (b.flags & 0x180000)
StartStrafe(b, (b.flags & 0x80000) != 0);
b.flags &= 0xFFE07FFF;
return (b.flags ^ entry) & 0xF;
}
// ---- triangle tests (0x006333D0 to 0x006337A5) -------------------------------------
// CMovement::BoxTopTouchesTriangle (0x006333D0).
bool TopTouches(const Body &b, const Facet &f) {
const double x = b.position[0], y = b.position[1], z = b.position[2], r = b.radius;
const float top = static_cast<float>(z + b.height);
const float corners[4][3] = {{static_cast<float>(x - r), static_cast<float>(y - r), top},
{static_cast<float>(x + r), static_cast<float>(y - r), top},
{static_cast<float>(x - r), static_cast<float>(y + r), top},
{static_cast<float>(x + r), static_cast<float>(y + r), top}};
for (const auto &c : corners)
if (std::fabs(Dot(f.plane.n, c) + f.plane.d) < kSkin)
return true;
return false;
}
// CMovement::ClassifyContactTriangle (0x006334A0), for a player-driven unit
// (0x005FA550 nonzero: the floor limit is cos 50°).
bool Classify(Body &b, const Facet &f, bool &steep) {
steep = false;
const float nz = f.plane.n[2];
if (nz > kCos50 || std::isnan(nz)) {
if (Contact::PointOverFacet(f, b.position))
b.flags &= ~FLAG_ON_STEP;
return true;
}
if (nz >= 0.0f)
return (b.flags & FLAG_ON_STEP) != 0;
if (!TopTouches(b, f))
return (b.flags & FLAG_ON_STEP) != 0;
if (b.flags & FLAG_ON_STEP) {
steep = true;
b.flags &= ~FLAG_ON_STEP;
}
const double up = -D(nz);
return up > kCos50 || std::isnan(up);
}
// IsListedTriangleSteep (0x006336A0).
bool Steep(float nz) {
if (nz > kCos50 || std::isnan(nz))
return false;
if (!(nz < 0.0f))
return true;
return !(-D(nz) > kCos50);
}
// CMovement::LandsOnTriangle (0x00633720).
bool LandsOn(const Body &b, const Facet &f, const float (&move)[3]) {
const float p[3] = {static_cast<float>(D(move[0]) + b.position[0]),
static_cast<float>(D(b.position[1]) + move[1]),
static_cast<float>(D(b.position[2]) + move[2])};
const float nz = f.plane.n[2];
if (!(nz > kCos50 || std::isnan(nz)))
return false;
return Contact::PointOverFacet(f, p);
}
// ---- walking helpers (0x00635C00 to 0x00636610) ------------------------------------
// SelectBevelContactNormal (0x00636610), transcribed rather than called: with
// three bevels and no odd one out the client reads an index from a stack slot
// it never wrote.
bool BevelDirection(const Plane *faces, uint32_t count, float (&out)[3]) {
if (count == 0 || count > 4)
return false;
for (uint32_t i = 0; i < count; ++i)
if (!(std::fabs(D(faces[i].n[2]) - kBevelZ) < kTiny))
return false;
switch (count) {
case 1:
std::memcpy(out, faces[0].n, sizeof out);
break;
case 2: {
const double sx = D(faces[1].n[0]) + faces[0].n[0];
const double sy = D(faces[1].n[1]) + faces[0].n[1];
const float sz = static_cast<float>(D(faces[1].n[2]) + faces[0].n[2]);
out[0] = static_cast<float>(sx * kBisect);
out[1] = static_cast<float>(sy * kBisect);
out[2] = static_cast<float>(D(sz) * kBisect);
break;
}
case 3: {
int alongX = -1, alongY = -1, countX = 0;
for (int i = 0; i < 3; ++i) {
if (std::fabs(faces[i].n[1]) < kTiny) {
alongX = i;
++countX;
} else if (alongY < 0 && std::fabs(faces[i].n[0]) < kTiny) {
alongY = i;
}
}
// Three bevels meet two along one axis: the odd one out. (The
// client reads an unwritten slot when none is; a body's own bevels
// always give one.)
const int pick = countX == 1 ? alongX : (alongY >= 0 ? alongY : 0);
std::memcpy(out, faces[pick].n, sizeof out);
break;
}
default:
out[0] = out[1] = out[2] = 0.0f;
break;
}
return true;
}
// CMovement::LimitFaceRise (0x00635C00): the rise of moving `length` along
// `dir` on the plane `n`, limited to the step height k.
void Rise(const Body &b, float k, const float (&dir)[3], float &length, float (&n)[3], bool flip,
const float (&bevel)[3], float (&out)[3]) {
const float bevel2 = static_cast<float>((D(bevel[0]) * bevel[0] + D(bevel[1]) * bevel[1]) + D(bevel[2]) * bevel[2]);
if (flip && !(n[2] > kCos50 || std::isnan(n[2])) && !(std::fabs(bevel2) < kParallel)) {
n[0] = -bevel[0];
n[1] = -bevel[1];
n[2] = -bevel[2];
}
const double p = ((-D(n[2]) * dir[2] + -D(n[1]) * dir[1]) + -D(n[0]) * dir[0]) * length;
float h;
if (std::fabs(n[2]) < kParallel)
h = p < 0.0 ? kNegFltMax : kFltMax;
else
h = static_cast<float>(p / n[2]);
float x;
if ((b.flags & FLAG_ON_STEP) && h < 0.0f) {
length = 0.0f;
x = k;
} else if (k < h) {
length = static_cast<float>((D(k) / h) * length);
x = k;
} else if (-D(k) > h) {
length = static_cast<float>(-((D(k) / h) * length));
x = -k;
} else {
x = h;
}
out[0] = out[1] = static_cast<float>(0.0 * x);
out[2] = x;
}
// ComputeWallPushOut (0x00635D80): the horizontal correction that cancels
// the part of the move entering the plane, plus 0.001.
void PushOut(const float (&w)[3], float length, const float (&n)[3], float (&out)[3]) {
if (n[2] < 0.0f && -D(n[2]) > kCos50) {
out[0] = out[1] = out[2] = 0.0f;
return;
}
const double lh = std::sqrt(D(n[0]) * n[0] + D(n[1]) * n[1]);
double ux = n[0], uy = n[1];
if (!(std::fabs(lh) < kParallel)) {
const double inv = 1.0 / lh;
ux = static_cast<float>(D(n[0]) * inv);
uy = D(n[1]) * inv;
}
const double p = ((-D(n[2]) * w[2] + -D(n[1]) * w[1]) + -D(n[0]) * w[0]) * length;
const double c = (uy * n[1] + ux * n[0]) + D(n[2]) * 0.0;
const double q = (std::fabs(c) < kParallel ? p : p / c) + kMsToSec;
out[0] = static_cast<float>(ux * q);
out[1] = static_cast<float>(uy * q);
out[2] = 0.0f;
}
// ---- the airborne resolver (0x00634960 to 0x00635600) ------------------------------
// CMovement::PlaneNearFootPoints (0x00634960).
bool PlaneNearFoot(const Body &b, const Plane &pl) {
const double x = b.position[0], y = b.position[1], z = b.position[2], r = b.radius;
const float waist = static_cast<float>(r * kWaist);
const float wz = static_cast<float>(z + waist);
const float points[5][3] = {{b.position[0], b.position[1], b.position[2]},
{static_cast<float>(x - r), static_cast<float>(y - r), wz},
{static_cast<float>(x + r), static_cast<float>(y - r), wz},
{static_cast<float>(x - r), static_cast<float>(y + r), wz},
{static_cast<float>(x + r), static_cast<float>(y + r), wz}};
for (const auto &p : points)
if (std::fabs(Dot(pl.n, p) + pl.d) < kSkin)
return true;
return false;
}
// IntersectThreePlanes: the point the three planes share.
void ThreePlanes(const Plane &a, const Plane &b, const Plane &c, float (&out)[3]) {
using Fn = float *(__fastcall *)(float *, const Plane *, const Plane *, const Plane *);
reinterpret_cast<Fn>(Offsets::FUN_INTERSECT_THREE_PLANES)(out, &a, &b, &c);
}
// PointToLineDistanceSquared: from p to the line through q along the unit
// `dir`.
double LineDistance2(const float (&p)[3], const float (&q)[3], const float (&dir)[3]) {
using Fn = double(__fastcall *)(const float *, const float *, const float *);
return reinterpret_cast<Fn>(Offsets::FUN_POINT_TO_LINE_DISTANCE_SQ)(p, q, dir);
}
// FindNearestTriangleEdge (0x00634DA0), which the client runs on its own
// facet list (0x00C4E534): transcribed for the body's list.
void NearestEdge(const Facet &f, const float (&point)[3], const float (&crease)[3], float (&out)[3]) {
float best = kFltMax;
out[0] = out[1] = out[2] = 0.0f;
for (int i = 0; i < 3; ++i) {
const float(&v)[3] = f.v[i];
const float(&next)[3] = f.v[(i + 1) % 3];
const double e[3] = {D(v[0]) - next[0], D(v[1]) - next[1], D(v[2]) - next[2]};
const double length = std::sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]);
if (std::fabs(length) < kParallel)
continue;
const double inv = 1.0 / length;
const float eh[3] = {static_cast<float>(e[0] * inv), static_cast<float>(e[1] * inv),
static_cast<float>(e[2] * inv)};
const double along = (D(eh[2]) * crease[2] + D(eh[1]) * crease[1]) + D(eh[0]) * crease[0];
double metric;
if (std::fabs(along - 1.0) < kTiny) {
metric = LineDistance2(v, point, crease);
} else {
const float k[3] = {static_cast<float>(D(eh[1]) * crease[2] - D(eh[2]) * crease[1]),
static_cast<float>(D(eh[2]) * crease[0] - D(eh[0]) * crease[2]),
static_cast<float>(D(eh[0]) * crease[1] - D(eh[1]) * crease[0])};
const double s = -((D(k[2]) * point[2] + D(k[1]) * point[1]) + D(k[0]) * point[0]);
const double m = s + ((D(k[2]) * v[2] + D(k[1]) * v[1]) + D(k[0]) * v[0]);
metric = m * m;
}
if (metric < best) {
best = static_cast<float>(metric);
std::memcpy(out, eh, sizeof out);
}
}
}
// CMovement::ComputeWallNormal (0x00634AE0).
void WallNormal(const Body &b, const Facet &f, const Plane *faces, uint32_t count, float (&out)[3]) {
const float(&e0)[3] = faces[0].n;
if (count == 3 || count == 4) {
std::memcpy(out, f.plane.n, sizeof out);
return;
}
if (count != 2) {
out[0] = -e0[0];
out[1] = -e0[1];
out[2] = -e0[2];
return;
}
const float(&e1)[3] = faces[1].n;
const float c0[3] = {static_cast<float>(D(e0[1]) * e1[2] - D(e0[2]) * e1[1]),
static_cast<float>(D(e0[2]) * e1[0] - D(e0[0]) * e1[2]),
static_cast<float>(D(e0[0]) * e1[1] - D(e0[1]) * e1[0])};
const double inv = 1.0 / std::sqrt((D(c0[0]) * c0[0] + D(c0[1]) * c0[1]) + D(c0[2]) * c0[2]);
const double cz = D(c0[2]) * inv;
const float c[3] = {static_cast<float>(D(c0[0]) * inv), static_cast<float>(D(c0[1]) * inv),
static_cast<float>(cz)};
const float(&n)[3] = f.plane.n;
if (std::fabs(cz) < kTiny || std::fabs((D(c[2]) * n[2] + D(c[1]) * n[1]) + D(c[0]) * n[0]) < kTiny) {
std::memcpy(out, n, sizeof out);
return;
}
if (!(std::fabs(D(c[2]) - 1.0) < kTiny) && PlaneNearFoot(b, f.plane)) {
std::memcpy(out, n, sizeof out);
return;
}
float corner[3], edge[3];
ThreePlanes(f.plane, faces[0], faces[1], corner);
NearestEdge(f, corner, c, edge);
const float m0[3] = {static_cast<float>(D(edge[1]) * c[2] - D(edge[2]) * c[1]),
static_cast<float>(D(edge[2]) * c[0] - D(edge[0]) * c[2]),
static_cast<float>(D(edge[0]) * c[1] - D(edge[1]) * c[0])};
const double ml = std::sqrt((D(m0[0]) * m0[0] + D(m0[1]) * m0[1]) + D(m0[2]) * m0[2]);
if (ml < kParallel) {
out[0] = -e0[0];
out[1] = -e0[1];
out[2] = -e0[2];
return;
}
const double mi = 1.0 / ml;
const float m[3] = {static_cast<float>(D(m0[0]) * mi), static_cast<float>(D(m0[1]) * mi),
static_cast<float>(D(m0[2]) * mi)};
const double d = (D(m[2]) * e0[2] + D(m[1]) * e0[1]) + D(m[0]) * e0[0];
const float k = d > 0.0 ? -1.0f : 1.0f;
out[0] = m[0] * k;
out[1] = m[1] * k;
out[2] = m[2] * k;
}
// CMovement::ComputeWallPush (0x00635090): the horizontal push that undoes
// the rest of the move's part into the wall.
void WallPush(const Body &b, const float (&u)[3], float a, float length, const Facet &f, const Plane *faces,
uint32_t count, float (&out)[2]) {
float w[3];
if (Steep(f.plane.n[2])) {
WallNormal(b, f, faces, count, w);
} else {
w[0] = -faces[0].n[0];
w[1] = -faces[0].n[1];
w[2] = -faces[0].n[2];
}
const double lh = std::sqrt(D(w[0]) * w[0] + D(w[1]) * w[1]);
double hx = w[0], hy = w[1];
if (!(std::fabs(lh) < kParallel)) {
const double inv = 1.0 / lh;
hx = static_cast<float>(D(w[0]) * inv);
hy = D(w[1]) * inv;
}
const double p = ((-D(w[2]) * u[2] + -D(w[1]) * u[1]) + -D(w[0]) * u[0]) * (D(length) - a);
const double den = (D(w[1]) * hy + hx * w[0]) + D(w[2]) * 0.0;
const double q = std::fabs(den) < kParallel ? p : p / den;
out[0] = static_cast<float>(hx * q);
out[1] = static_cast<float>(hy * q);
}
bool AnyFaceZ(const Plane *faces, uint32_t count, float z) {
for (uint32_t i = 0; i < count && i < 9; ++i)
if (std::fabs(D(faces[i].n[2]) - z) < kTiny)
return true;
return false;
}
// One airborne step (0x006351A0): 0 clear, 1 hit, 2 the sweep failed.
int AirStep(Body &b, float t, float (&move)[3], float &a, float (&push)[2], bool &land, bool &head) {
land = head = false;
const float length = a;
if (std::fabs(length) < kParallel)
return 0;
const double inv = 1.0 / length;
const float u[3] = {static_cast<float>(inv * move[0]), static_cast<float>(inv * move[1]),
static_cast<float>(inv * move[2])};
const Hit hit = Sweep(b, u, length);
a = hit.distance;
if (!hit.ok) {
move[0] = move[1] = move[2] = 0.0f;
return 2;
}
if (hit.index >= FacetCount(b))
return 0;
move[0] = static_cast<float>(D(u[0]) * a);
move[1] = static_cast<float>(D(u[1]) * a);
move[2] = static_cast<float>(D(a) * u[2]);
const Facet &f = b.facets.solid[hit.index];
if (LandsOn(b, f, move)) {
land = true;
return 1;
}
if (u[2] > 0.0f) {
const double r = ApexTime(b) - t;
if (r >= 0.0 && r * b.speed >= a && AnyFaceZ(hit.faces, hit.count, 1.0f)) {
head = true;
return 1;
}
}
WallPush(b, u, a, length, f, hit.faces, hit.count, push);
return 1;
}
// CMovement::IsRisingThroughStep (0x00635550).
bool RisingThrough(const Body &b, float t, double remaining, const float (&move)[3], bool head) {
if (head)
return true;
if (b.fallSpeed >= 0.0f)
return false;
const float apex = static_cast<float>(ApexTime(b));
if (D(t) + remaining < apex)
return true;
if (t > apex)
return false;
const double m = (D(apex) - t) * b.speed;
return m * m > D(move[1]) * move[1] + D(move[0]) * move[0];
}
// CMovement::LimitStepByFall (0x00635450): the step's time from the arc.
double StepTime(Body &b, double remaining, float t, float (&move)[3], float &a, bool head, float horizontal) {
const bool rising = RisingThrough(b, t, remaining, move, head);
float k = 0.0f;
if (horizontal > kTiny)
k = static_cast<float>((std::sqrt(D(move[1]) * move[1] + D(move[0]) * move[0]) / horizontal) * remaining);
const float drop = static_cast<float>((D(b.fallStartZ) - b.position[2]) - move[2]);
const double arc = FallTime(b, drop, rising);
if (arc <= t) {
move[0] = move[1] = move[2] = 0.0f;
a = 0.0f;
return 0.0;
}
const double d = arc - t;
if (d > remaining)
return remaining;
if (d < k) {
const double s = d / k;
const double sx = s * move[0], sy = s * move[1];
move[0] = static_cast<float>(sx);
move[1] = static_cast<float>(sy);
a = static_cast<float>(std::sqrt((sy * sy + sx * sx) + D(move[2]) * move[2]));
}
return d;
}
// CMovement::ResolveAirborneMove (0x00635600).
uint32_t Airborne(Body &b, uint32_t ms, const float (&moveIn)[3], bool report) {
const float total = static_cast<float>(D(static_cast<float>(ms)) * kMsToSec);
const float fallStart = static_cast<float>(D(static_cast<float>(b.fallTime)) * kMsToSec);
float used = 0.0f;
const float savedSpeed = b.speed;
const float savedHeading[2] = {b.heading[0], b.heading[1]};
float savedDirection[3];
std::memcpy(savedDirection, b.direction, sizeof savedDirection);
const float start[2] = {b.position[0], b.position[1]};
float move[3] = {moveIn[0], moveIn[1], moveIn[2]};
float length = static_cast<float>(std::sqrt((D(move[0]) * move[0] + D(move[1]) * move[1]) + D(move[2]) * move[2]));
const float h0 = static_cast<float>(std::sqrt(D(move[1]) * move[1] + D(move[0]) * move[0]));
const float z0 = static_cast<float>(std::fabs(D(move[2])));
float horizontal = h0;
float push[2] = {0.0f, 0.0f};
uint32_t stalls = 0;
bool forced = false, continued = false, failed = false, head = false;
double finish = total;
for (;;) {
if (std::fabs(length) < kTiny)
break;
const double inv = D(kOne) / length;
const float u[3] = {static_cast<float>(inv * move[0]), static_cast<float>(inv * move[1]),
static_cast<float>(inv * move[2])};
float a = length;
const float t = static_cast<float>(D(fallStart) + used);
bool land;
const int step = AirStep(b, t, move, a, push, land, head);
const double remaining = D(total) - used;
if (step == 2) {
failed = true;
break;
}
const double tau = step == 1 ? StepTime(b, remaining, t, move, a, head, horizontal) : remaining;
for (int k = 0; k < 3; ++k)
b.position[k] = static_cast<float>(D(move[k]) + b.position[k]);
used = static_cast<float>(D(used) + tau);
if (head) {
RestartFallAtRest(b);
finish = used;
break;
}
if (land) {
Land(b);
finish = used;
break;
}
if (D(used) + kStall >= total)
break;
length = static_cast<float>(D(length) - a);
continued = true;
if (!(tau > kStall)) {
if (++stalls > 5) {
if (forced || std::fabs(horizontal) < kTiny) {
Land(b);
finish = used;
break;
}
// A forced drop: straight down for the rest.
b.speed = 0.0f;
const double dz = D(u[2]) * length;
move[0] = move[1] = 0.0f;
move[2] = static_cast<float>(dz);
stalls = 0;
forced = true;
length = static_cast<float>(std::fabs(dz));
continue;
}
} else {
stalls = 1;
}
move[0] = static_cast<float>(D(u[0]) * length + push[0]);
move[1] = static_cast<float>(D(u[1]) * length + push[1]);
move[2] = static_cast<float>(D(u[2]) * length);
if (stalls >= 2) {
const float ax = static_cast<float>((D(b.position[0]) - start[0]) + move[0]);
const double ay = (D(b.position[1]) - start[1]) + move[1];
const double reach = std::sqrt(ay * ay + D(ax) * ax);
if (reach > D(h0) + kTiny && reach > D(z0) * kStallReach) {
Land(b);
finish = used;
break;
}
}
// The heading after contact.
const double flat2 = D(move[1]) * move[1] + D(move[0]) * move[0];
horizontal = static_cast<float>(std::sqrt(flat2));
float nx, ny;
if (std::fabs(horizontal) < kTiny) {
nx = move[0];
ny = move[1];
b.speed = 0.0f;
} else {
const double invH = D(kOne) / horizontal;
nx = static_cast<float>(D(move[0]) * invH);
const double ny53 = invH * move[1];
ny = static_cast<float>(ny53);
const double v = (ny53 * b.heading[1] + D(nx) * b.heading[0]) * b.speed;
b.speed = static_cast<float>(v);
if (v < 0.0)
b.speed = 0.0f;
}
b.heading[0] = nx;
b.heading[1] = ny;
b.direction[0] = nx;
b.direction[1] = ny;
b.direction[2] = 0.0f;
length = static_cast<float>(std::sqrt(flat2 + D(move[2]) * move[2]));
}
if (failed) {
const int32_t lost = Round((D(total) - used) * kThousand);
b.elapsed -= static_cast<uint32_t>(lost);
b.fallTime -= static_cast<uint32_t>(lost);
finish = total;
}
const int32_t n = Round(finish * kThousand);
if (!head)
b.fallTime += static_cast<uint32_t>(n);
if (report) {
if (!(b.flags & FLAG_FALLING)) {
if (ApplyLatches(b) != 0)
return static_cast<uint32_t>(n);
} else {
LatchFallingFar(b);
if ((b.flags & FLAG_FALLING) && !(b.fallSpeed == 0.0f) && (b.flags & 0xF)) {
b.speed = savedSpeed;
b.heading[0] = savedHeading[0];
b.heading[1] = savedHeading[1];
std::memcpy(b.direction, savedDirection, sizeof b.direction);
}
}
}
if (continued)
Anchor(b, false);
return static_cast<uint32_t>(n);
}
// ---- the walk resolver (0x00635F80 to 0x006367B0) ----------------------------------
// The 56 bytes 0x00635EA0 saves and 0x00635F10 restores.
struct Motion {
float anchor[3], anchorFacing, anchorPitch;
uint32_t elapsed, fallTime;
float direction[3], heading[2];
uint32_t flags;
float speed;
};
Motion SaveMotion(const Body &b) {
Motion m;
std::memcpy(m.anchor, b.anchor, sizeof m.anchor);
m.anchorFacing = b.anchorFacing;
m.anchorPitch = b.anchorPitch;
m.elapsed = b.elapsed;
m.fallTime = b.fallTime;
std::memcpy(m.direction, b.direction, sizeof m.direction);
std::memcpy(m.heading, b.heading, sizeof m.heading);
m.flags = b.flags;
m.speed = b.speed;
return m;
}
void RestoreMotion(Body &b, const Motion &m) {
std::memcpy(b.anchor, m.anchor, sizeof b.anchor);
b.anchorFacing = m.anchorFacing;
b.anchorPitch = m.anchorPitch;
b.elapsed = m.elapsed;
b.fallTime = m.fallTime;
std::memcpy(b.direction, m.direction, sizeof b.direction);
std::memcpy(b.heading, m.heading, sizeof b.heading);
b.flags = m.flags;
b.speed = m.speed;
}
// CMovement::ProbeFallAway (0x00635F80): whether a free fall of `drop`
// carries the body a radius from `start` within 10° of `dir`. The position,
// +0x7C and +0xA0 stay as the simulated fall left them.
bool FallAway(Body &b, const float (&dir)[2], const float (&start)[2], float drop) {
const double h = D(drop) + kTenth;
const uint32_t total = static_cast<uint32_t>(Round(std::sqrt((h + h) * kInvG) * kThousand));
const Motion saved = SaveMotion(b);
StartFall(b, 0.0f);
const float move[3] = {b.heading[0], b.heading[1], static_cast<float>(-FallDrop(b, total))};
for (uint32_t done = 0; done < total && (b.flags & FLAG_FALLING);)
done += Airborne(b, total - done, move, false);
RestoreMotion(b, saved);
const float dx = static_cast<float>(D(b.position[0]) - start[0]);
const double dy53 = D(b.position[1]) - start[1];
const float dy = static_cast<float>(dy53);
const double distance = std::sqrt(dy53 * dy + D(dx) * dx);
if (distance < b.radius)
return false;
const double inv = D(kOne) / distance;
const double ux = D(dx) * inv, uy = D(dy) * inv;
return D(dir[1]) * uy + ux * dir[0] > kCos10;
}
// The step height (0x00617430): +0xB8 for a player-driven unit.
float StepHeight(const Body &b) { return b.stepRatio; }
// CMovement::ProbeStepUp (0x00636100): 0 a sweep failed, 1 blocked, 2 the
// step is allowed.
int StepUp(Body &b, const float (&along)[2], const float (&n)[3]) {
const float saved[3] = {b.position[0], b.position[1], b.position[2]};
const float k = StepHeight(b);
const float rPlus = static_cast<float>(D(b.radius) + kSkin);
const float reach = D(k) * kTan50 > rPlus ? static_cast<float>(D(k) * kTan50) : rPlus;
float sx = along[0], sy = along[1];
if (!(n[2] > kCos50 || std::isnan(n[2]) || n[2] < 0.0f)) {
const double inv = 1.0 / std::sqrt(D(n[1]) * n[1] + D(n[0]) * n[0]);
sx = -static_cast<float>(inv * n[0]);
sy = -static_cast<float>(D(n[1]) * inv);
const float back[3] = {sx, sy, 0.0f};
const Hit probe = Sweep(b, back, reach);
if (!probe.ok)
return 0;
const bool same = probe.index < FacetCount(b) && b.facets.solid[probe.index].plane.n[0] == n[0] &&
b.facets.solid[probe.index].plane.n[1] == n[1] &&
b.facets.solid[probe.index].plane.n[2] == n[2];
if (!same) {
sx = along[0];
sy = along[1];
}
}
const float upDir[3] = {0.0f, 0.0f, 1.0f};
const Hit upHit = Sweep(b, upDir, k);
if (!upHit.ok)
return 0;
float up = upHit.distance;
if (std::fabs(up) < kParallel)
return 1;
b.position[2] = static_cast<float>(D(b.position[2]) + up);
const float across[3] = {sx, sy, 0.0f};
const Hit over = Sweep(b, across, reach);
if (!over.ok)
return 0;
const float f = over.distance;
b.position[0] = static_cast<float>(D(b.position[0]) + D(sx) * f);
b.position[1] = static_cast<float>(D(b.position[1]) + D(sy) * f);
uint32_t last = over.index;
float travelled = f;
if (!(std::fabs(D(f) - reach) < kTiny) && over.index < FacetCount(b)) {
const Facet &face = b.facets.solid[over.index];
bool steep;
if (Classify(b, face, steep)) {
float length = static_cast<float>(D(reach) - f);
float bevel[3] = {0.0f, 0.0f, 0.0f};
BevelDirection(over.faces, over.count, bevel);
float n2[3] = {face.plane.n[0], face.plane.n[1], face.plane.n[2]};
float rise[3];
Rise(b, k, across, length, n2, true, bevel, rise);
const float nv[3] = {static_cast<float>(D(rise[0]) + D(sx) * length),
static_cast<float>(D(rise[1]) + D(sy) * length), rise[2]};
const double l = std::sqrt((D(nv[0]) * nv[0] + D(nv[1]) * nv[1]) + D(nv[2]) * nv[2]);
const float lf = static_cast<float>(l);
if (!(std::fabs(l) < kParallel)) {
const double inv = 1.0 / lf;
const float nd[3] = {static_cast<float>(D(nv[0]) * inv), static_cast<float>(D(nv[1]) * inv),
static_cast<float>(inv * nv[2])};
const Hit slide = Sweep(b, nd, lf);
if (!slide.ok)
return 0;
const double dx = D(nd[0]) * slide.distance, dy = D(nd[1]) * slide.distance;
const float dz = static_cast<float>(D(nd[2]) * slide.distance);
travelled = static_cast<float>(D(f) + std::sqrt(dx * dx + dy * dy));
b.position[0] = static_cast<float>(D(b.position[0]) + dx);
b.position[1] = static_cast<float>(D(b.position[1]) + dy);
b.position[2] = static_cast<float>(D(b.position[2]) + dz);
up = static_cast<float>(D(up) + dz);
last = slide.index;
}
}
}
const auto allowed = [&] {
std::memcpy(b.position, saved, sizeof b.position);
if (!(b.flags & FLAG_ON_STEP))
b.stepFloor = saved[2];
return 2;
};
if (last != FacetCount(b) && !(travelled > b.radius)) {
std::memcpy(b.position, saved, sizeof b.position);
return 1;
}
const float downDir[3] = {0.0f, 0.0f, -1.0f};
const Hit down = Sweep(b, downDir, static_cast<float>(D(up) + kDownBias));
if (!down.ok)
return 0;
b.position[2] = static_cast<float>(D(b.position[2]) - down.distance);
if (down.index >= FacetCount(b))
return allowed();
const float floorZ = b.facets.solid[down.index].plane.n[2];
if (floorZ > kCos50 || std::isnan(floorZ))
return allowed();
const float drop = static_cast<float>(D(up) - down.distance);
const float from[2] = {saved[0], saved[1]};
const float way[2] = {sx, sy};
if (FallAway(b, way, from, drop))
return allowed();
std::memcpy(b.position, saved, sizeof b.position);
return 1;
}
// CMovement::ResolveWalk (0x006367B0): a grounded body moved `dist` along
// (dir, 0) within `ms`. The hover branches are left out.
uint32_t Walk(Body &b, uint32_t ms, float dist, const float (&dir)[2]) {
if (std::fabs(dist) < kTiny)
return ms;
if (FacetCount(b) == 0) {
StartFall(b, 0.0f);
return 0;
}
const float total = static_cast<float>(ms);
const float k = StepHeight(b);
float slide[3] = {dir[0], dir[1], 0.0f};
float hx = dir[0], hy = dir[1];
float length = dist, budget = dist, used = 0.0f, segment = total;
bool reanchor = false, noFloor = false;
uint32_t shortHits = 0;
Hit hit = Sweep(b, slide, dist);
if (!hit.ok)
return ms;
bool settle = false;
for (;;) {
float delta[3];
for (int i = 0; i < 3; ++i)
delta[i] = static_cast<float>(D(slide[i]) * hit.distance);
for (int i = 0; i < 3; ++i)
b.position[i] = static_cast<float>(D(delta[i]) + b.position[i]);
if (hit.index >= FacetCount(b)) {
used = static_cast<float>(D(segment) + used);
const double rest = D(total) - used;
if (rest < 1.0 || std::isnan(rest)) {
settle = true;
break;
}
segment = static_cast<float>(rest);
slide[0] = dir[0];
slide[1] = dir[1];
slide[2] = 0.0f;
hx = dir[0];
hy = dir[1];
length = static_cast<float>(rest / total * dist);
} else {
const double dt = D(hit.distance) / length * segment;
length = static_cast<float>(D(length) - hit.distance);
budget = static_cast<float>(D(budget) - std::sqrt(D(delta[0]) * delta[0] + D(delta[1]) * delta[1]));
if (dt < 1.0 || std::isnan(dt)) {
if (++shortHits > 5) {
reanchor = true;
break;
}
} else {
shortHits = 1;
}
used = static_cast<float>(D(used) + dt);
segment = static_cast<float>(D(segment) - dt);
if (D(total) - used < 1.0) {
if (!(std::fabs(D(total) - used) < kTiny))
reanchor = true;
break;
}
const Facet &face = b.facets.solid[hit.index];
bool steep;
const bool walkable = Classify(b, face, steep);
noFloor = steep;
float response[3];
float bevel[3] = {0.0f, 0.0f, 0.0f};
float n[3] = {face.plane.n[0], face.plane.n[1], face.plane.n[2]};
if (walkable) {
const float before = length;
const bool flip = BevelDirection(hit.faces, hit.count, bevel);
Rise(b, k, slide, length, n, flip, bevel, response);
segment = static_cast<float>(D(length) / before * segment);
} else {
if (steep)
break;
reanchor = true;
const bool flip = BevelDirection(hit.faces, hit.count, bevel);
const float along[2] = {hx, hy};
const int code = StepUp(b, along, n);
if (code == 0)
return ms;
if (code == 2) {
b.flags |= FLAG_ON_STEP;
const float before = length;
Rise(b, k, slide, length, n, flip, bevel, response);
segment = static_cast<float>(D(length) / before * segment);
} else {
PushOut(slide, length, n, response);
}
}
// The deflected move.
const float sz = static_cast<float>(D(slide[2]) * length);
const float mx = static_cast<float>(D(slide[0]) * length + response[0]);
const float my = static_cast<float>(D(response[1]) + D(slide[1]) * length);
const float mz = static_cast<float>(D(response[2]) + sz);
const float mx2 = static_cast<float>(D(mx) * mx);
const float my2 = static_cast<float>(D(my) * my);
const double newLength = std::sqrt((D(mz) * mz + my2) + mx2);
length = static_cast<float>(newLength);
if (newLength < kMsToSec) {
reanchor = true;
break;
}
hx = mx;
hy = my;
const double inv = D(kOne) / length;
slide[0] = static_cast<float>(D(mx) * inv);
slide[1] = static_cast<float>(D(my) * inv);
slide[2] = static_cast<float>(inv * mz);
const double oldBudget = budget;
const double flat = std::sqrt(D(my2) + mx2);
budget = static_cast<float>(flat);
if (!(std::fabs(flat) < kParallel)) {
const double invH = D(kOne) / budget;
hx = static_cast<float>(D(mx) * invH);
hy = static_cast<float>(D(my) * invH);
}
if (oldBudget - flat > kTiny)
reanchor = true;
// A rise past the step's top is cut to it.
if ((b.flags & FLAG_ON_STEP) && mz > kTiny) {
const float target = static_cast<float>(D(mz) + b.position[2]);
if (D(k) + b.stepFloor < target) {
const double room = D(k) + b.stepFloor - b.position[2];
length = static_cast<float>(room / mz * length);
if (!(std::fabs(room - mz) < kTiny))
reanchor = true;
if (length < kMsToSec) {
noFloor = true;
break;
}
}
}
}
hit = Sweep(b, slide, length);
if (!hit.ok)
return ms;
}
if (settle) {
// Down onto the ground.
float reachDown = static_cast<float>(D(hit.distance) * kWaist + kThirtySixth);
if (b.flags & FLAG_ON_STEP)
reachDown = static_cast<float>(D(k) + reachDown);
const float downDir[3] = {0.0f, 0.0f, -1.0f};
hit = Sweep(b, downDir, reachDown);
if (!hit.ok)
return ms;
b.position[2] = static_cast<float>(D(b.position[2]) - hit.distance);
if (hit.index >= FacetCount(b)) {
noFloor = true;
} else {
float n[3];
std::memcpy(n, b.facets.solid[hit.index].plane.n, sizeof n);
if (n[2] > kCos50 || std::isnan(n[2])) {
b.flags &= ~FLAG_ON_STEP;
} else {
const float along[2] = {hx, hy};
const int code = StepUp(b, along, n);
if (code == 0)
return ms;
if (code == 1)
noFloor = true;
else
b.flags |= FLAG_ON_STEP;
}
}
}
if (hit.index >= FacetCount(b) || noFloor) {
StartFall(b, 0.0f);
return ms;
}
if (reanchor)
Anchor(b, false);
return ms;
}
// CMovement fall step (0x00635B00).
uint32_t FallStep(Body &b, uint32_t ms, float dist, const float (&dir)[2]) {
if (ms == 0)
return 0;
const double negDrop = -FallDrop(b, b.fallTime + ms);
const float dz = static_cast<float>(negDrop);
const float dx = static_cast<float>(D(dist) * dir[0]);
const float dy = static_cast<float>(D(dist) * dir[1]);
const double magnitude = std::sqrt((negDrop * dz + D(dy) * dy) + D(dx) * dx);
if (std::fabs(magnitude) < kParallel) {
b.fallTime += ms;
return ms;
}
if (FacetCount(b) == 0) {
b.position[0] = static_cast<float>(D(dx) + b.position[0]);
b.position[1] = static_cast<float>(D(dy) + b.position[1]);
b.position[2] = static_cast<float>(D(dz) + b.position[2]);
b.fallTime += ms;
LatchFallingFar(b);
return ms;
}
const float move[3] = {dx, dy, dz};
return Airborne(b, ms, move, true);
}
// ---- the substep loop (0x00633840, 0x00634040) -------------------------------------
void Union(float (&box)[6], const float (&other)[6]) {
for (int i = 0; i < 3; ++i) {
if (box[i] > other[i])
box[i] = other[i];
if (other[i + 3] > box[i + 3])
box[i + 3] = other[i + 3];
}
}
// CMovement::QuerySweptBox (0x00633840): the facets in the body's box grown
// along the substep's move, and a swimmer's liquid in the same box.
bool QuerySwept(Body &b, float dist, uint32_t ms, const float (&u)[3]) {
const double r = b.radius;
float box[6];
Contact::BaseBox(b.position, b.radius, b.height, box);
float m[3] = {static_cast<float>(D(u[0]) * dist), static_cast<float>(D(u[1]) * dist),
static_cast<float>(D(u[2]) * dist)};
if (b.flags & FLAG_FALLING) {
m[2] = static_cast<float>(D(m[2]) - FallDrop(b, b.fallTime + ms));
float moved[6];
for (int i = 0; i < 6; ++i)
moved[i] = static_cast<float>(D(box[i]) + m[i % 3]);
Union(box, moved);
const double t = D(m[2]) * kNegTan50;
if (t > 0.0) {
for (int i = 0; i < 2; ++i) {
box[i] = static_cast<float>(D(box[i]) - t);
box[i + 3] = static_cast<float>(D(box[i + 3]) + t);
}
}
} else if (b.flags & FLAG_SWIMMING) {
// The box about the move's midpoint, half the move each way, widened
// to the body's diagonal and raised by its height (0x00633B5E).
const float half = static_cast<float>(D(dist) * kHalf);
float c[3];
for (int i = 0; i < 3; ++i)
c[i] = static_cast<float>(D(static_cast<float>(D(u[i]) * half)) + b.position[i]);
const float q = static_cast<float>(r * kSqrt2);
float around[6];
for (int i = 0; i < 3; ++i) {
around[i] = static_cast<float>(D(c[i]) - half);
around[i + 3] = static_cast<float>(D(c[i]) + half);
}
around[0] = static_cast<float>(D(around[0]) - q);
around[3] = static_cast<float>(D(around[3]) + q);
around[1] = static_cast<float>(D(around[1]) - q);
around[4] = static_cast<float>(D(around[4]) + q);
const float h = b.height;
around[5] = static_cast<float>(D(h < q ? q : h) + around[5]);
Union(box, around);
} else {
const double k = StepHeight(b);
const float rPlus = static_cast<float>(r + kSkin);
const double grow = k * kTan50 > rPlus ? k * kTan50 : D(rPlus);
float moved[6];
for (int i = 0; i < 6; ++i)
moved[i] = static_cast<float>(D(box[i]) + static_cast<float>(D(u[i % 3]) * (grow + dist)));
Union(box, moved);
const float half = static_cast<float>(D(dist) * kHalf);
float centre[3];
for (int i = 0; i < 3; ++i)
centre[i] = static_cast<float>(D(static_cast<float>(D(u[i]) * half)) + b.position[i]);
const double lateral = r * kSqrt2 + half;
const float side[6] = {static_cast<float>(D(centre[0]) - lateral), static_cast<float>(D(centre[1]) - lateral),
centre[2], static_cast<float>(D(centre[0]) + lateral),
static_cast<float>(D(centre[1]) + lateral), centre[2]};
Union(box, side);
const double twoK = k + k;
box[5] = static_cast<float>((twoK > dist ? twoK : D(dist)) + box[5]);
box[2] = static_cast<float>(D(box[2]) - (k + D(dist) * kTan50));
}
for (int i = 0; i < 3; ++i) {
box[i] = static_cast<float>(D(box[i]) - kSkin);
box[i + 3] = static_cast<float>(D(box[i + 3]) + kSkin);
}
if (!b.facets.query(b.facets.context, box, b.facets.solid))
return false;
if (b.flags & FLAG_SWIMMING)
Contact::QueryLiquid(b.facets, box);
std::memcpy(b.facets.box, box, sizeof box);
return true;
}
// CMovement's slide off a facet (0x00634300): the part of `rem` along `u`
// that goes into facet `hit` of `list`, pushed back out along its normal.
void SlideOff(const std::vector<Facet> &list, uint32_t hit, const float (&u)[3], float rem, float (&out)[3]) {
const Plane &n = list[hit].plane;
const double k = ((-D(n.n[2]) * u[2] + -D(n.n[1]) * u[1]) + -D(n.n[0]) * u[0]) * rem;
const float kf = static_cast<float>(k);
out[0] = static_cast<float>(D(kf) * n.n[0]);
out[1] = static_cast<float>(D(kf) * n.n[1]);
out[2] = static_cast<float>(k * n.n[2]);
}
// Moves the body `a` along `u` (0x00634774, 0x006347AC): x and y at 53 bits,
// z through a float.
void MoveAlong(Body &b, const float (&u)[3], float a) {
const float dz = static_cast<float>(D(u[2]) * a);
b.position[0] = static_cast<float>(D(u[0]) * a + b.position[0]);
b.position[1] = static_cast<float>(D(u[1]) * a + b.position[1]);
b.position[2] = static_cast<float>(D(dz) + b.position[2]);
}
// CMovement::ResolveFreeMove (0x00634640): a swimmer's move, `dist` along
// `u` over `ms`, with no gravity and no steps: it sweeps the solid list, and
// while no solid facet has stopped it in this call the liquid list for what
// is left, and slides along what it meets until the time is used, the move
// is spent, or five passes in a row make no progress. Every end but a
// failed sweep re-anchors, except a move that ended before any slide
// without a hit or with no time left. It reports all of `ms` used.
uint32_t FreeMove(Body &b, uint32_t ms, float dist, float (&u)[3]) {
if (ms == 0)
return 0;
if (std::fabs(dist) < kTiny)
return ms;
const float total = static_cast<float>(ms);
float used = 0.0f;
float rem = dist;
uint32_t stalls = 0;
bool solidHit = false, slid = false;
Hit h = Sweep(b, u, dist);
if (!h.ok)
return ms;
for (;;) {
bool liquid = false;
if (h.index < FacetCount(b)) {
solidHit = true;
} else {
if (!solidHit && !(b.flags & 0x800)) {
h = Sweep(b, u, rem, true);
if (!h.ok)
return ms;
liquid = true;
}
const std::vector<Facet> &list = liquid ? b.facets.liquid : b.facets.solid;
if (h.index == list.size()) {
MoveAlong(b, u, h.distance);
if (slid)
Anchor(b, false);
return ms;
}
}
MoveAlong(b, u, h.distance);
const double t = (D(total) - used) * (D(h.distance) / rem);
rem = static_cast<float>(D(rem) - h.distance);
if (D(kTiny) + t > kOne) {
used = static_cast<float>(t + used);
stalls = 1;
} else if (++stalls > 5) {
break;
}
if (D(total) - used < kOne) {
if (!(std::fabs(D(total) - used) < kTiny) || slid)
break;
return ms;
}
float push[3];
SlideOff(liquid ? b.facets.liquid : b.facets.solid, h.index, u, rem, push);
slid = true;
const float dz = static_cast<float>(D(u[2]) * rem);
const float m[3] = {static_cast<float>(D(u[0]) * rem + push[0]),
static_cast<float>(D(push[1]) + D(u[1]) * rem), static_cast<float>(D(push[2]) + dz)};
rem = static_cast<float>(std::sqrt((D(m[2]) * m[2] + D(m[1]) * m[1]) + D(m[0]) * m[0]));
if (std::fabs(rem) < kTiny)
break;
const double inverse = D(kOne) / rem;
for (int i = 0; i < 3; ++i)
u[i] = static_cast<float>(D(m[i]) * inverse);
h = Sweep(b, u, rem);
if (!h.ok)
return ms;
}
Anchor(b, false);
return ms;
}
// ---- the spline (0x00453300 to 0x00454820, 0x007C5490, 0x00616AF0 to 0x00616D60) ---------
//
// A ground spline's curve is the Catmull-Rom curve in mode 0: control
// point i + 1 to i + 2 is segment i, a straight chord, and the curve is
// walked by arc length. Its first and last control points are guards.
// LocateCatmullRomByLength (0x00454380 -> 0x00453840): the segment and the
// part of it at arc-length fraction `t`.
void Locate(const Spline &s, float t, uint32_t &seg, float &frac) {
const uint32_t n = static_cast<uint32_t>(s.points.size()) - 3;
if (n == 1) {
seg = 0;
frac = t;
return;
}
const double at = D(s.length) * t;
float running = 0.0f;
uint32_t i = 0;
if (n - 1 != 0) {
for (;;) {
const double next = D(s.segments[i]) + running;
if (at < next)
break;
++i;
running = static_cast<float>(next);
if (i >= n - 1)
break;
}
}
seg = i;
frac = static_cast<float>((at - running) / s.segments[i]);
}
// EvaluateCurvePosition (0x004541B0), mode 0: the chord's point at `f`.
void ChordPoint(const Spline &s, uint32_t seg, float f, float (&out)[3]) {
const std::array<float, 3> &a = s.points[seg + 1];
const std::array<float, 3> &c = s.points[seg + 2];
const double dx = D(c[0]) - a[0];
const double dy = D(c[1]) - a[1];
const float dz = static_cast<float>(D(c[2]) - a[2]);
const float xf = static_cast<float>(dx * f);
out[0] = static_cast<float>(D(xf) + a[0]);
out[1] = static_cast<float>(dy * f + a[1]);
out[2] = static_cast<float>(D(dz) * f + a[2]);
}
// CatmullRomSegmentLength (0x00454320), mode 0: the chord's length.
double ChordLength(const Spline &s, uint32_t seg) {
const std::array<float, 3> &a = s.points[seg + 1];
const std::array<float, 3> &c = s.points[seg + 2];
const double dx = D(c[0]) - a[0];
const double dy = D(c[1]) - a[1];
const double dz = D(c[2]) - a[2];
return std::sqrt((dz * dz + dy * dy) + dx * dx);
}
// The curve's points (0x00453360 -> slot 7 0x00454770, then 0x004532E0):
// with more than three, each segment's length (slot 1 0x004543E0) and their
// sum (slot 0 0x004543D0 -> 0x00453920) as the total.
void SetPath(Spline &s, const std::vector<std::array<float, 3>> &points) {
s.points = points;
s.segments.clear();
if (s.points.size() <= 3)
return;
const uint32_t n = static_cast<uint32_t>(s.points.size()) - 3;
s.segments.resize(n);
for (uint32_t i = 0; i < n; ++i)
s.segments[i] = static_cast<float>(ChordLength(s, i));
double total = 0.0;
for (float len : s.segments)
total += len;
s.length = static_cast<float>(total);
}
// The frame at `t` (EvaluateSplineFrame 0x00453480 -> CatmullRomFrame
// 0x00454580), mode 0: the unit chord as the tangent, and the point. The
// follower reads no other row on the ground.
void Frame(const Spline &s, float t, float (&tangent)[3], float (&point)[3]) {
if (t < 0.0f)
t = 0.0f;
else if (!(t < 1.0f)) // 1.0 and above, and NaN
t = 1.0f;
uint32_t seg = 0;
float frac = 0.0f;
Locate(s, t, seg, frac);
ChordPoint(s, seg, frac, point);
const std::array<float, 3> &a = s.points[seg + 1];
const std::array<float, 3> &c = s.points[seg + 2];
float chord[3] = {static_cast<float>(D(c[0]) - a[0]), static_cast<float>(D(c[1]) - a[1]),
static_cast<float>(D(c[2]) - a[2])};
const double len = std::sqrt((D(chord[0]) * chord[0] + D(chord[1]) * chord[1]) + D(chord[2]) * chord[2]);
if (!(std::fabs(len) < kParallel)) { // 0x004549C0 by the stored length
const double inv = D(kOne) / static_cast<float>(len);
for (float &v : chord)
v = static_cast<float>(v * inv);
}
std::memcpy(tangent, chord, sizeof chord);
}
// CMovement::FollowSpline (0x007C5490): the spline's progress at `now`, the
// direction and facing along it (and the pitch for a swimmer), and the
// point less the anchor. Without a direction bit it returns the flags'
// 0x200F and writes nothing.
uint32_t FollowSpline(Body &b, uint32_t now, float (&out)[3]) {
if (!(b.flags & 0x3))
return b.flags & 0x200F;
Spline &s = *b.spline;
if (s.flags & kSplineHeld)
return 1;
s.elapsed = static_cast<int32_t>(now - s.start);
float u;
if (s.duration == 0) {
u = 1.0f;
} else if (s.elapsed < 0) {
u = 0.0f;
} else if (static_cast<uint32_t>(s.elapsed) < s.duration) {
u = static_cast<float>(static_cast<double>(s.elapsed) / static_cast<int32_t>(s.duration));
} else {
u = 1.0f;
s.flags |= kSplineArrived;
}
float tangent[3];
std::memcpy(tangent, b.direction, sizeof tangent);
float point[3] = {0.0f, 0.0f, 0.0f};
Frame(s, u, tangent, point);
const double facing = std::atan2(D(tangent[1]), D(tangent[0]));
b.facing = static_cast<float>(facing);
if (facing < 0.0)
b.facing = static_cast<float>(facing + kTwoPi);
std::memcpy(b.direction, tangent, sizeof tangent);
if (b.flags & FLAG_SWIMMING)
b.pitch = static_cast<float>(std::asin(D(tangent[2])));
for (int i = 0; i < 3; ++i)
out[i] = static_cast<float>(D(point[i]) - b.anchor[i]);
return 1;
}
// CMovement::GetSplineStep (0x00616AF0): the spline's displacement, or,
// for a horizontal step past 3 yards or 60 yards a second, the point taken
// at once without the collision, which ends the step (0).
uint32_t SplineStep(Body &b, uint32_t end, uint32_t length, float (&delta)[3]) {
if (FollowSpline(b, end, delta) == 0)
return 0;
if (!(b.flags & 0x200F))
return 1;
float target[3];
for (int i = 0; i < 3; ++i)
target[i] = static_cast<float>(D(b.anchor[i]) + delta[i]);
const double dx = D(target[0]) - b.position[0];
const double dy = D(target[1]) - b.position[1];
const double d = dy * dy + dx * dx;
const double t = static_cast<double>(length) * kMsToSec;
// Greater, or NaN, takes the point (0x00616B88, 0x00616B95).
if (d / (t * t) <= kWarpSpeedSq && d <= kSnapSq)
return 1;
std::memcpy(b.position, target, sizeof target);
if (b.flags & FLAG_FALLING) // GetFallStartZ (0x007C5F30)
b.fallStartZ = static_cast<float>(FallDistanceMs(b, b.fallTime) + b.position[2]);
return 0;
}
// CMovement::SnapIfFar (0x00616D60): to `p` when it is 3 yards away or
// more, or NaN, ending any fall.
void SnapIfFar(Body &b, const float (&p)[3]) {
const double dx = D(p[0]) - b.position[0];
const double dy = D(p[1]) - b.position[1];
const double dz = D(p[2]) - b.position[2];
if ((dz * dz + dx * dx) + dy * dy < kSnapSq)
return;
std::memcpy(b.position, p, sizeof b.position);
Land(b);
}
// CompleteSpline (0x00619DE0) for the active mover: done, reported
// (CMSG 0x2C9, the caller's) and freed.
void CompleteSpline(Body &b) {
b.spline->flags |= kSplineDone;
ClearSpline(b);
}
// The spline's part of BroadcastStateChange (0x0061A820) after a substep
// that used up its time (`rest` 0): at the end of the duration the body
// stops at the last point, takes the final facing and completes the
// spline. True when it arrived. (A completed spline is freed, so the
// client's branch for one marked done never runs here.)
bool SplineArrival(Body &b, uint32_t rest) {
if (rest != 0 || !Live(b) || !(b.flags & 0x3) || !(b.spline->flags & kSplineArrived))
return false;
StopMoveNow(b);
SnapIfFar(b, b.spline->end);
if (b.spline->flags & kSplineFinalAngle)
b.facing = b.spline->facingAngle;
CompleteSpline(b);
return true;
}
// CMovement::StopAllMotion (0x0061AA70): no fall, no latch, no turn, pitch,
// strafe or move. The unit side picks its animation after.
void StopAllMotion(Body &b) {
Land(b);
b.flags &= 0xFBE07FFF;
StopTurn(b);
StopPitch(b);
if (b.flags & 0xC)
StopStrafeNow(b);
StopMoveNow(b);
b.flags &= 0xFFFF9FFF;
}
// CMovement::ResolveDisplacement (0x00634040): `d` over `ms` in substeps.
uint32_t Resolve(Body &b, uint32_t ms, const float (&d)[3]) {
if (ms == 0)
return 0;
const double length = std::sqrt((D(d[0]) * d[0] + D(d[1]) * d[1]) + D(d[2]) * d[2]);
const float speed = static_cast<float>(length / (D(static_cast<float>(ms)) * kMsToSec));
float u[3] = {0.0f, 0.0f, 0.0f};
if (!(std::fabs(length) < kTiny)) {
const double inv = D(kOne) / length;
for (int i = 0; i < 3; ++i)
u[i] = static_cast<float>(inv * d[i]);
}
uint32_t done = 0;
for (int pass = 0; pass < kMaxPasses && (b.flags & 0x200F); ++pass) {
const uint32_t rest = ms - done;
const float dist = static_cast<float>((D(static_cast<float>(rest)) * kMsToSec) * speed);
if (!QuerySwept(b, dist, rest, u)) {
b.elapsed -= rest;
done = ms;
break;
}
const uint32_t oldFlags = b.flags;
// The loop's own test: a start speed of exactly 0, or NaN, is no jump.
const bool jumped = (oldFlags & FLAG_FALLING) && (b.fallSpeed < 0.0f || b.fallSpeed > 0.0f);
const float savedSpeed = b.speed;
const float flat[2] = {u[0], u[1]};
if (b.flags & 0x200800) {
float free[3] = {u[0], u[1], u[2]};
done += FreeMove(b, rest, dist, free);
} else {
done += (b.flags & FLAG_FALLING) ? FallStep(b, rest, dist, flat) : Walk(b, rest, dist, flat);
}
const bool arrived = SplineArrival(b, ms - done);
if (b.stepped != nullptr)
b.stepped(b.steppedContext, oldFlags, jumped, arrived);
// A live spline sets the speed itself (0x0063426F).
if (!Live(b) && !(b.speed == savedSpeed)) {
RefreshSpeed(b, false);
break;
}
if (b.elapsed == 0 && (b.flags & 0xF))
b.elapsed = ms - done;
if (done >= ms)
break;
}
return done;
}
// The position check (0x00616BF0).
bool PositionValid(const Body &b) {
for (float c : b.position)
if (!std::isfinite(c))
return false;
const double y = D(kCellCentre) - b.position[1];
if (y < 2.0 || !(y < kCellSpan))
return false;
const float x = static_cast<float>(D(kCellCentre) - b.position[0]);
return !(x < 2.0f) && x < kCellSpan;
}
// The substep apply (0x00616CB0): the anchor plus the displacement, less
// the position; no vertical part on the ground or in the air.
uint32_t Apply(Body &b, uint32_t r, const float (&delta)[3]) {
const double tx = D(b.anchor[0]) + delta[0];
const double ty = D(b.anchor[1]) + delta[1];
const float tz = static_cast<float>(D(b.anchor[2]) + delta[2]);
float d[3] = {static_cast<float>(tx - b.position[0]), static_cast<float>(ty - b.position[1]),
static_cast<float>(D(tz) - b.position[2])};
if (!(b.flags & 0x200800))
d[2] = 0.0f;
return Resolve(b, r, d);
}
// The substeps of one step (0x00616DE0) ending at the tick `end`: by the
// live spline, else by the motion since the anchors. After them a body the
// collision left 3 yards or more off its spline goes to the spline's point.
void Integrate(Body &b, uint32_t end, uint32_t length) {
if (!PositionValid(b) || length == 0)
return;
uint32_t done = 0;
float onSpline[3] = {0.0f, 0.0f, 0.0f};
for (int pass = 0; pass < kMaxPasses && (b.flags & 0x20FF); ++pass) {
const uint32_t r = length - done;
b.elapsed += r;
float delta[3] = {0.0f, 0.0f, 0.0f};
if (Live(b)) {
if (SplineStep(b, end, r, delta) == 0)
return;
for (int i = 0; i < 3; ++i)
onSpline[i] = static_cast<float>(D(delta[i]) + b.anchor[i]);
} else if (!Displacement(b, b.elapsed, delta)) {
return;
}
done += Apply(b, r, delta);
if (done >= length)
break;
}
if (PositionValid(b) && Live(b))
SnapIfFar(b, onSpline);
}
} // namespace
void Init(Body &body, const float (&p)[3], float facing, uint32_t now) {
body = Body{};
std::memcpy(body.position, p, sizeof body.position);
std::memcpy(body.anchor, p, sizeof body.anchor);
body.facing = body.anchorFacing = facing;
body.pitchBasis[0] = 1.0f;
body.fallStartZ = p[2];
body.walkSpeed = kWalkSpeed;
body.runSpeed = kRunSpeed;
body.runBackSpeed = kRunBackSpeed;
body.swimSpeed = kSwimSpeed;
body.swimBackSpeed = kSwimBackSpeed;
body.turnRate = kTurnRate;
body.radius = kDefaultRadius;
body.height = kDefaultHeight;
body.stepRatio = 1.0f;
body.clock = now;
Direction(body, false);
}
void SetCollisionSize(Body &body, float width, float height, float scale, float displayScale) {
body.stepRatio = std::fabs(displayScale) < kParallel ? 1.0f : static_cast<float>(D(scale) / displayScale);
body.radius = static_cast<float>(D(width) * scale * kHalf);
body.height = static_cast<float>(D(height) * scale);
}
bool Tick(Body &body, uint32_t now) {
if (static_cast<int32_t>(now - body.clock) <= 0)
return false;
uint32_t window = now - body.clock;
body.clock = now;
if (window > kMaxWindowMs)
window = kMaxWindowMs; // the client reports the rest as skipped (0x00615AE0)
if (body.flags & 0x20FF)
Integrate(body, now, window);
return true;
}
// The position reset frees a live spline (0x007C69A0 with free_spline 1).
void Reset(Body &body, const float (&p)[3], float facing) {
std::memcpy(body.position, p, sizeof body.position);
body.facing = facing;
body.pitch = 0.0f;
body.flags &= 0xFBC07F00;
Land(body);
Anchor(body, false);
RefreshSpeed(body, false);
if (Live(body))
ClearSpline(body);
}
void SetFacing(Body &body, float facing) {
if (!(std::fabs(D(facing) - body.facing) < kTiny)) {
body.facing = facing;
if (!(body.flags & FLAG_FALLING))
Anchor(body, false);
}
body.flags &= ~0x30u;
}
bool FallGate(Body &body) {
if (body.flags & 0x203800)
return false;
StartFall(body, 0.0f);
return true;
}
bool StartMove(Body &body, bool forward) {
body.flags &= 0xFFF97FFF;
const bool falling = (body.flags & FLAG_FALLING) != 0;
if (falling && (body.flags & 0xF)) {
if (forward) {
if (body.flags & FLAG_FORWARD)
return false;
body.flags |= 0x20000;
} else {
if (body.flags & FLAG_BACKWARD)
return false;
body.flags |= 0x40000;
}
return false;
}
body.flags = forward ? (body.flags & ~FLAG_BACKWARD) | FLAG_FORWARD : (body.flags & ~FLAG_FORWARD) | FLAG_BACKWARD;
Anchor(body, falling);
RefreshSpeed(body, falling);
return true;
}
bool StopMove(Body &body) {
if (!(body.flags & 0x3)) {
body.flags &= 0xFFF9FFFF;
return false;
}
if (body.flags & FLAG_ON_STEP) {
body.flags &= ~FLAG_ON_STEP;
FallGate(body);
}
if (body.flags & FLAG_FALLING) {
body.flags = (body.flags & 0xFFF9FFFF) | 0x8000;
return false;
}
StopMoveNow(body);
return true;
}
bool StartStrafe(Body &body, bool left) {
body.flags &= 0xFFE6FFFF;
const bool falling = (body.flags & FLAG_FALLING) != 0;
if (falling && (body.flags & 0xF)) {
if (left) {
if (body.flags & FLAG_STRAFE_LEFT)
return false;
body.flags |= 0x80000;
} else {
if (body.flags & FLAG_STRAFE_RIGHT)
return false;
body.flags |= 0x100000;
}
return false;
}
body.flags = left ? (body.flags & ~FLAG_STRAFE_RIGHT) | FLAG_STRAFE_LEFT
: (body.flags & ~FLAG_STRAFE_LEFT) | FLAG_STRAFE_RIGHT;
Anchor(body, falling);
RefreshSpeed(body, falling);
return true;
}
bool StopStrafe(Body &body) {
if (!(body.flags & 0xC)) {
body.flags &= 0xFFE7FFFF;
return false;
}
if (body.flags & FLAG_ON_STEP) {
body.flags &= ~FLAG_ON_STEP;
FallGate(body);
}
if (body.flags & FLAG_FALLING) {
body.flags = (body.flags & 0xFFE7FFFF) | 0x10000;
return false;
}
StopStrafeNow(body);
return true;
}
void StartTurn(Body &body, bool left) {
body.flags = left ? (body.flags & ~FLAG_TURN_RIGHT) | FLAG_TURN_LEFT : (body.flags & ~FLAG_TURN_LEFT) | FLAG_TURN_RIGHT;
Anchor(body, false);
}
bool StopTurn(Body &body) {
if (!(body.flags & 0x30))
return false;
body.flags &= ~0x30u;
Anchor(body, false);
return true;
}
bool Jump(Body &body) {
if (body.flags & 0x40000000)
return false;
if (body.flags & 0x3000)
return false;
StartFall(body, (body.flags & FLAG_SWIMMING) ? kSwimJumpSpeed : kJumpSpeed);
return true;
}
void SetRunMode(Body &body, bool run) {
if (run)
body.flags &= ~FLAG_WALK;
else
body.flags |= FLAG_WALK;
Anchor(body, false);
RefreshSpeed(body, false);
}
namespace {
float &SpeedField(Body &b, Speed which) {
switch (which) {
case Speed::Walk:
return b.walkSpeed;
case Speed::Run:
return b.runSpeed;
case Speed::RunBack:
return b.runBackSpeed;
case Speed::Swim:
return b.swimSpeed;
case Speed::SwimBack:
return b.swimBackSpeed;
case Speed::Turn:
break;
}
return b.turnRate;
}
} // namespace
bool SetSpeed(Body &body, Speed which, float value) {
float &field = SpeedField(body, which);
if (std::fabs(D(value) - field) < kSpeedTiny)
return false;
field = value;
Anchor(body, false);
if (which != Speed::Turn)
RefreshSpeed(body, false);
return true;
}
float GetSpeed(const Body &body, Speed which) { return SpeedField(const_cast<Body &>(body), which); }
void ClearSpline(Body &body) {
if (!body.spline)
return;
body.spline.reset();
RefreshSpeed(body, false);
}
bool OnSpline(const Body &body) { return Live(body); }
namespace {
using Point = std::array<float, 3>;
// InitSpline (0x007C62C0): the record, made when absent, with no flags and
// `p` as its last point.
void InitSpline(Body &b, const float (&p)[3]) {
if (!b.spline)
b.spline.emplace();
b.spline->flags = 0;
std::memcpy(b.spline->end, p, sizeof b.spline->end);
}
// CMovement::StartSplinePath (0x006187A0), after the queue's flush: every
// motion stops; unless rooted the spline starts at the tick (StartSpline
// 0x007C6A50), its run mode set from flag 0x100, and the body moves
// forward. Its last point is the curve's at 1.0 (0x00453390), the last
// control point. (Deactivate 0x00619E80 would only mark a running spline
// done, which InitSpline overwrites.)
bool StartSplinePath(Body &b, const std::vector<Point> &points, uint32_t duration, uint32_t flags) {
StopAllMotion(b);
if (b.flags & 0x1000)
return false;
const float last[3] = {points.back()[0], points.back()[1], points.back()[2]};
InitSpline(b, last);
Spline &s = *b.spline;
s.flags = flags;
s.start = b.clock;
s.elapsed = 0;
s.duration = duration;
SetPath(s, points);
SetRunMode(b, (flags & kSplineRun) != 0);
StartMove(b, true);
std::memcpy(s.end, last, sizeof s.end);
return true;
}
// CMovement::FlushAndStopSpline (0x00618750): stopped where it stands, the
// spline ending at `p` finished (FinishSpline 0x00616D30) and completed.
void StopSplineAt(Body &b, const float (&p)[3]) {
StopAllMotion(b);
InitSpline(b, p);
b.spline->points.clear(); // ResetSplinePath (0x007C6A20): no points
b.spline->segments.clear();
SnapIfFar(b, b.spline->end);
b.spline->elapsed = static_cast<int32_t>(b.spline->duration);
b.spline->flags |= kSplineArrived;
CompleteSpline(b);
}
// SetWorldFacing (0x007C6EB0) with no transport: wrapped once into [0, 2pi].
void SetWorldFacing(Body &b, float facing) {
if (facing <= kTwoPi) {
if (facing < 0.0f)
facing = static_cast<float>(D(kTwoPi) + facing);
SetFacing(b, facing);
return;
}
SetFacing(b, static_cast<float>(D(facing) - kTwoPi));
}
// __ftol by fistp under round to nearest; out of range gives 0x80000000.
int32_t RoundNearest(float x) {
const double r = std::nearbyint(D(x));
if (!(r >= -2147483648.0 && r <= 2147483647.0))
return INT32_MIN;
return static_cast<int32_t>(r);
}
} // namespace
bool MoveTo(Body &body, const float (&to)[3], const float *facing) {
// The move as a server sends it: from where the body stands, with the
// body's run mode, and the time the path takes at the body's own speed.
const uint32_t flags = (body.flags & FLAG_WALK) ? 0 : kSplineRun;
const float serverSpeed = (body.flags & FLAG_SWIMMING) ? body.swimSpeed
: (body.flags & FLAG_WALK) ? body.walkSpeed
: body.runSpeed;
// The path (0x00601D41): the start, and the end unless within 1/36 yard
// of it. AnchorSplinePath (0x006021C0) keeps a path that starts where the
// body stands as it is, and empties a lone point there.
std::vector<Point> path{{body.position[0], body.position[1], body.position[2]}};
{
const double dx = D(to[0]) - body.position[0];
const double dy = D(to[1]) - body.position[1];
const double dz = D(to[2]) - body.position[2];
if ((dx * dx + dz * dz) + dy * dy > kReachedSq)
path.push_back({to[0], to[1], to[2]});
}
if (path.size() > 1) {
// The guards (0x00601E66 to 0x00601ECA): the last point again, and
// before the first the second reflected through it.
const Point &p = path[0], &q = path[1];
const float dz = static_cast<float>(D(q[2]) - p[2]);
std::vector<Point> points{{static_cast<float>(D(p[0]) - (D(q[0]) - p[0])),
static_cast<float>(D(p[1]) - (D(q[1]) - p[1])),
static_cast<float>(D(p[2]) - dz)}};
points.insert(points.end(), path.begin(), path.end());
points.push_back(path.back());
// The length (0x00601EF4), summed from the last chord.
double length = 0.0;
for (size_t i = points.size() - 2; i > 1; --i) {
const Point &lo = points[i - 1], &hi = points[i];
const double dx = D(hi[0]) - lo[0], dy = D(hi[1]) - lo[1], dz2 = D(hi[2]) - lo[2];
length += std::sqrt((dx * dx + dz2 * dz2) + dy * dy);
}
if (length > kSixth) {
const uint32_t sent =
serverSpeed > 0.0f ? static_cast<uint32_t>(length / serverSpeed * kThousand) : 0;
// The speed (0x00601F3D): the path's own when slower than four
// times the run speed.
double speed = D(body.runSpeed) * kFour;
if (sent != 0) {
const double pathSpeed = length / (static_cast<double>(sent) * kMsToSec);
if (pathSpeed < speed)
speed = static_cast<float>(pathSpeed);
}
if (speed > kTiny) {
// The duration (0x00601F9F to 0x00601FCC), 1 ms at least.
const int32_t ms = RoundNearest(static_cast<float>((length / speed) * kThousand));
const uint32_t duration = ms <= 1 ? 1u : static_cast<uint32_t>(ms);
if (StartSplinePath(body, points, duration, flags)) {
if (facing != nullptr) { // SetSplineFacingAngle (0x006188A0)
body.spline->flags |= kSplineFinalAngle;
body.spline->facingAngle = *facing;
}
return true;
}
}
}
}
// No path (0x0060205E): the facing now, and the spline done at once.
if (facing != nullptr)
SetWorldFacing(body, *facing);
StopSplineAt(body, to);
return false;
}
void StartPitch(Body &body, bool up) {
if (!(body.flags & FLAG_SWIMMING))
return;
body.flags = up ? (body.flags & ~FLAG_PITCH_DOWN) | FLAG_PITCH_UP : (body.flags & ~FLAG_PITCH_UP) | FLAG_PITCH_DOWN;
Anchor(body, false);
}
bool StopPitch(Body &body) {
if (!(body.flags & 0xC0))
return false;
body.flags &= ~0xC0u;
Anchor(body, false);
return true;
}
namespace {
// IsRising (0x007C5DE0): in the air, still short of the jump's apex.
bool Rising(const Body &b) {
if (!(b.flags & FLAG_FALLING) || b.fallSpeed == 0.0f)
return false;
const float t = static_cast<float>(static_cast<double>(b.fallTime) * kMsToSec);
return ApexTime(b) > t;
}
// Swimming on (0x007C6E50): a landing in the water, at the swim speed.
void StartSwimming(Body &b) {
b.flags = (b.flags & ~FLAG_ON_STEP) | FLAG_SWIMMING;
Anchor(b, false);
Land(b);
RefreshSpeed(b, false);
}
// Swimming off (0x007C6E80): level, then a fall from rest.
void StopSwimming(Body &b) {
b.flags &= 0xFFDFFF3F;
b.anchorPitch = 0.0f;
b.pitch = 0.0f;
FallGate(b);
RefreshSpeed(b, false);
}
} // namespace
void KnockBack(Body &body, float cosine, float sine, float speedXY, float speedZ) {
if (body.flags & FLAG_SWIMMING)
StopSwimming(body);
StartFall(body, speedZ);
// The fall start re-anchored with the old flags; the pair overwrites the
// direction it computed (0x006179E5 to 0x00617A04).
body.heading[0] = cosine;
body.heading[1] = sine;
body.direction[0] = cosine;
body.direction[1] = sine;
body.direction[2] = 0.0f;
body.flags = (body.flags & ~FLAG_BACKWARD) | FLAG_STOP_ON_LAND | FLAG_FORWARD;
body.speed = speedXY;
}
bool UpdateSwimState(Body &body, bool inLiquid, float depth) {
if (body.flags & 0x400)
return false;
const double enter = D(body.height) * kThreeQuarters;
const float leave = static_cast<float>(enter - kThirtySixth);
if (!(body.flags & FLAG_SWIMMING)) {
// The active mover's start request (0x00619FB0) is applied as
// EnterSwim (0x0061A230): swimming on, then the latched directions.
if (enter < depth && !Rising(body)) {
StartSwimming(body);
ApplyLatches(body);
}
// The splash (0x0060314A): the depth past two fifths of the height
// now and at the last test differ (a NaN on neither side), then the
// depth is kept.
const double splashAt = D(body.height) * kTwoFifths;
const bool now = D(depth) > splashAt;
const bool before = splashAt < D(body.splashDepth);
body.splashDepth = depth;
return now != before;
}
if (!inLiquid || !(depth >= leave))
StopSwimming(body);
return false;
}
float ModelPitch(const Body &body) {
if (!(body.flags & FLAG_SWIMMING) || !(body.flags & 0x3))
return 0.0f;
return static_cast<float>(D(kTwoPi) - body.pitch); // 2π at 0x0080295C
}
float FallSpeed(const Body &body) {
const double cap = Terminal(body);
const float t = static_cast<float>(D(static_cast<float>(body.fallTime)) * kMsToSec);
const float w = static_cast<float>(D(t) * kG + CappedFallSpeed(body));
return cap < w ? static_cast<float>(cap) : w;
}
float CurrentSpeed(const Body &body) { return SelectSpeed(body, false); }
} // namespace Model::Scene::Mover