Files
TeslaRel410/restoration/source410/BT/MISSILE.CPP
T
CydandClaude Fable 5 2f0d3b6239 BT410 Phase 5.3.23: the cylinder hit-location table -- unaimed hits land WHERE THEY STRIKE
DMGTABLE born (type-29 streams, BT411 byte-verified format): the height x
angle grid -- DamageLookupTable rows by height, PieSlice cells by angle
(rotate-with-torso rows add the live twist), DamageZonePercentTable = the
cumulative hit-distribution roll (first threshold above the roll -- the
BattleTech dice scatter).  Loaded in the Mech ctor by the DamageZoneStream
member's NAME (mech+0x444, resident); Mech::TakeDamageMessageHandler
resolves invalidDamageZone hits through it (binary @0x4a0264 tail).

The missile fuse becomes the authentic unaimed producer: zone -1 + the
round's world position as the impact point (ENTITY3.HPP: "damage zones are
only valid via reticle based weapons").

Fight-verified on the authentic 7-row bhk1 table: flat-flying SRMs strike
low -> row 0 -> FEET AND LEG zones, side-correct by impact angle (+x right,
-x left), identical geometry spread across the cell distribution by the
roll.  17/17 missile lifecycle, zero faults; smoke + novice green.  Also
fixed: the ctor's reservedState zero-loop counted past the shrunken array.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 14:46:39 -05:00

375 lines
12 KiB
C++

//===========================================================================//
// File: missile.cpp //
// Project: BattleTech Brick: Mech weapons //
// Contents: Missile -- the guided self-propelled projectile entity //
//---------------------------------------------------------------------------//
// Copyright (C) 1995, Virtual World Entertainment, Inc. //
// All Rights reserved worldwide //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL //
//===========================================================================//
//
// RECONSTRUCTED against the binary via the BT411 decomp (ctor @004bf5b4,
// MoveAndCollide @004bef78, allocator @004bf8bc) and the surviving
// SEEKER.HPP / MISTHRST.HPP. See MISSILE.NOTES.md for the staged pieces
// (flight constants pending the missile GameModel record decode; world
// collision + explosion entity are the render/effects wave).
//
#include <bt.hpp>
#pragma hdrstop
#if !defined(MISSILE_HPP)
# include <missile.hpp>
#endif
#if !defined(SEEKER_HPP)
# include <seeker.hpp>
#endif
#if !defined(MISTHRST_HPP)
# include <misthrst.hpp>
#endif
#if !defined(MECH_HPP)
# include <mech.hpp>
#endif
#if !defined(RANDOM_HPP)
# include <random.hpp>
#endif
#if !defined(APP_HPP)
# include <app.hpp>
#endif
//
// Guidance tuning (binary .rdata @004bf594..@004bf5b0, BT411-decoded).
// STAGED-DEFAULT flight envelope: lifetime / thrust come from the missile
// GameModel record (model +0x44 / +0x48) once that layout is decoded; until
// then the defaults below fly a believable SRM (fast, short-burn).
//
static const Scalar MissileSteerEps = 1.0e-4f; // _DAT_004bf598 steering deadband
static const Scalar MissileTurnGain = 4.0f; // _DAT_004bf5a4 turn rate gain (1/s)
static const Scalar MissileDriftGain = 0.1f; // _DAT_004bf5a8
static const Scalar DefaultLifetime = 10.0f; // model +0x44 (staged default)
static const Scalar DefaultFuseRadius = 20.0f; // proximity fuse (staged)
Derivation
Missile::ClassDerivations(
Projectile::ClassDerivations,
"Missile"
);
Missile::SharedData
Missile::DefaultData(
Missile::ClassDerivations,
Mover::MessageHandlers,
Mover::AttributeIndex,
1,
(Entity::MakeHandler)Missile::Make
);
Missile*
Missile::Make(MakeMessage *creation_message)
{
return new Missile(creation_message);
}
//
//#############################################################################
// Construction (binary @004bf5b4). Chains the Projectile base (Entity
// transform + Mover motion/model stream -- the MakeMessage's resourceID is
// the launcher's explosion/projectile GameModel). The subsystem roster is
// built in Launch(), which carries the target the binary's spawn descriptor
// delivered to the ctor.
//#############################################################################
//
Missile::Missile(
MakeMessage *creation_message,
SharedData &shared_data
):
Projectile(creation_message, shared_data)
{
Check(creation_message);
lifetime = DefaultLifetime;
ageFraction = 0.0f;
age = 0.0f;
detonationFlag = 0;
flightVelocity = Vector3D(0.0f, 0.0f, 0.0f);
fuseRadius = DefaultFuseRadius;
launcherEntityID = EntityID::Null;
telemetryCountdown = 0;
//
// The authored flight envelope, from our family's GameModel record (the
// binary's ResolveModel). The 'mslhit' record is 8 bytes:
// { int 498, Scalar maxTimeOfFlight } -- SRM reads 5.0s (range 800 at
// ~160 u/s checks out). Sanity-banded: the owner-family fallback's
// GameModel is a MECH record whose +4 is a turn rate, and the band
// keeps the default in that case.
//
{
ResourceDescription *game_model =
application->GetResourceFile()->SearchList(
resourceID,
ResourceDescription::GameModelResourceType
);
if (game_model != NULL && game_model->resourceSize >= 8)
{
game_model->Lock();
Scalar authored =
((Scalar *)game_model->resourceAddress)[1];
if (authored > 0.5f && authored < 30.0f)
{
lifetime = authored;
}
game_model->Unlock();
}
}
Check_Fpu();
}
Missile::~Missile()
{
if (getenv("BT_MECH_LOG"))
{
DEBUG_STREAM << "[msl] dtor (death row fry)" << endl << flush;
}
}
Logical
Missile::TestInstance() const
{
return IsDerivedFrom(ClassDerivations);
}
//
//#############################################################################
// Launch -- seed the flight (the binary ctor tail + spawn-descriptor
// unpack). Builds the two-subsystem roster (Seeker @004bec34 with the
// homing target, MissileThruster @004be7c4), aims the initial velocity at
// the target and installs the guided integrator as the per-frame
// Performance. Tail = SetValidFlag(), like every 1995 entity.
//#############################################################################
//
void
Missile::Launch(
Mech *launcher,
Entity *target,
Damage &damage,
Scalar launch_speed
)
{
Check(this);
Check(launcher);
launcherEntityID = launcher->GetEntityID();
damageData = damage;
//
// The 2-entry subsystem roster (binary this[0x49] = 2).
//
subsystemCount = SubsystemCount;
subsystemArray = new Subsystem *[SubsystemCount];
Register_Pointer(subsystemArray);
subsystemArray[SeekerSubsystem] =
new Seeker(this, SeekerSubsystem, NULL, target,
Point3D(0.0f, 0.0f, 0.0f));
Register_Object(subsystemArray[SeekerSubsystem]);
subsystemArray[MissileThrusterSubsystem] =
new MissileThruster(this, MissileThrusterSubsystem, NULL);
Register_Object(subsystemArray[MissileThrusterSubsystem]);
//
// Initial velocity: straight at the target's current position (the
// authored MuzzleVelocity up-tilt arc through the MOUNT frame is the
// muzzle wave -- the seeker converges either way).
//
if (target != NULL)
{
Vector3D aim;
aim.Subtract(
target->localOrigin.linearPosition,
localOrigin.linearPosition
);
Scalar range = aim.Length();
if (range > 0.01f)
{
flightVelocity.x = aim.x / range * launch_speed;
flightVelocity.y = aim.y / range * launch_speed;
flightVelocity.z = aim.z / range * launch_speed;
}
}
SetPerformance(&Missile::MoveAndCollide);
AlwaysExecute();
SetValidFlag();
if (getenv("BT_MECH_LOG"))
{
DEBUG_STREAM << "[msl] LAUNCH from " << launcherEntityID
<< " at (" << localOrigin.linearPosition.x
<< "," << localOrigin.linearPosition.y
<< "," << localOrigin.linearPosition.z
<< ") spd=" << launch_speed
<< " dmg=" << damageData.damageAmount
<< "x" << damageData.burstCount << endl << flush;
}
}
//
//#############################################################################
// MoveAndCollide (binary @004bef78) -- the guided per-frame integrator.
//
// 1. Age; past the lifetime the round fizzles (death row, no damage).
// 2. Re-lead the target (Seeker::FindTarget) + bleed the thruster burn.
// 3. Steer the velocity toward the Seeker's lead point (turn gain 4.0,
// deadband 1e-4) and integrate the position.
// 4. Proximity fuse: inside fuseRadius of the aim point the round
// DETONATES -- the cluster damage record lands on the target (random
// hull zone interim, same as the direct path) and the round retires.
// World-geometry collision (@0042291c) + the explosion entity
// (@004be078, ClassID 0x5C) are the render/effects wave.
//#############################################################################
//
void
Missile::MoveAndCollide(Scalar time_slice)
{
Check(this);
age += time_slice;
ageFraction = (age <= lifetime) ? age / lifetime : 1.0f;
if (age > lifetime)
{
if (getenv("BT_MECH_LOG"))
{
DEBUG_STREAM << "[msl] fizzle (lifetime) at ("
<< localOrigin.linearPosition.x << ","
<< localOrigin.linearPosition.y << ","
<< localOrigin.linearPosition.z << ")" << endl << flush;
}
CondemnToDeathRow();
Check_Fpu();
return;
}
Seeker *seeker =
(Seeker *)subsystemArray[SeekerSubsystem];
MissileThruster *thruster =
(MissileThruster *)subsystemArray[MissileThrusterSubsystem];
Check(seeker);
Check(thruster);
//
// The hosted subsystems are advanced FROM the integrator (the binary
// folds their work into this pass -- no separate roster tick).
//
seeker->FindTarget(time_slice);
thruster->MissileThrusterSimulation(time_slice);
//
// Steering: bend the velocity toward the seeker's lead point at
// MissileTurnGain per second, holding speed; the thruster adds pace
// while burning.
//
Vector3D toLead;
toLead.Subtract(seeker->targetPosition, localOrigin.linearPosition);
Scalar range = toLead.Length();
Scalar speed = flightVelocity.Length();
if (thruster->burnTimeRemaining > 0.0f)
{
speed += thruster->thrusterAcceleration * time_slice;
}
if (range > MissileSteerEps)
{
Scalar blend = MissileTurnGain * time_slice;
if (blend > 1.0f)
{
blend = 1.0f;
}
Vector3D desired;
desired.x = toLead.x / range * speed;
desired.y = toLead.y / range * speed;
desired.z = toLead.z / range * speed;
flightVelocity.x += (desired.x - flightVelocity.x) * blend;
flightVelocity.y += (desired.y - flightVelocity.y) * blend;
flightVelocity.z += (desired.z - flightVelocity.z) * blend;
}
localOrigin.linearPosition.x += flightVelocity.x * time_slice;
localOrigin.linearPosition.y += flightVelocity.y * time_slice;
localOrigin.linearPosition.z += flightVelocity.z * time_slice;
localToWorld = localOrigin;
//
// The proximity fuse (the BANKED seeker notes: fuse on the seeker's
// rangeToTarget).
//
seeker->rangeToTarget = range;
if (range <= fuseRadius && seeker->targetEntity != NULL)
{
Entity *victim = seeker->targetEntity;
//
// The AUTHENTIC unaimed delivery: zone -1 (invalidDamageZone) with
// the round's world position as the impact point -- the victim's
// cylinder hit-location table resolves WHERE the round physically
// struck ("For BattleTech, damage zones are only valid via reticle
// based weapons" -- the surviving ENTITY3.HPP warning). Without a
// table the base handler drops the hit, so victims with no
// type-29 stream fall back to the interim random-zone pick.
//
int zone = -1;
damageData.impactPoint = localOrigin.linearPosition;
if (victim->IsDerivedFrom(Mech::ClassDerivations))
{
Mech *mech_victim = (Mech *)victim;
if (mech_victim->GetDamageLookupTable() == NULL
&& victim->damageZoneCount > 0)
{
zone = Random(victim->damageZoneCount);
}
}
else if (victim->damageZoneCount > 0)
{
zone = Random(victim->damageZoneCount);
}
Entity::TakeDamageMessage
hit(
Entity::TakeDamageMessageID,
sizeof(Entity::TakeDamageMessage),
launcherEntityID,
zone,
damageData
);
victim->Dispatch(&hit);
if (getenv("BT_MECH_LOG"))
{
DEBUG_STREAM << "[msl] DETONATE on zone " << zone
<< " amount=" << damageData.damageAmount
<< " burst=" << damageData.burstCount
<< " after " << age << "s" << endl << flush;
}
CondemnToDeathRow();
Check_Fpu();
return;
}
if (getenv("BT_MECH_LOG") && --telemetryCountdown <= 0)
{
telemetryCountdown = 30;
DEBUG_STREAM << "[msl] t=" << age
<< " pos=(" << localOrigin.linearPosition.x
<< "," << localOrigin.linearPosition.y
<< "," << localOrigin.linearPosition.z
<< ") range=" << range
<< " spd=" << speed << endl << flush;
}
Check_Fpu();
}