Files
BT412/game/reconstructed/heat.hpp
T
arcattackandClaude Opus 4.8 7b7d465e5e Initial commit: bt411 -- standalone Windows BattleTech (Tesla 4.10 port)
Clean, self-contained extraction of the BattleTech-specific work from the
reverse-engineering workspace -- engine + game + content + build, with nothing
from Red Planet or the raw archive dumps. Builds green (Win32) and runs the
single-player drive->animate->target->fire->damage->destroy loop out of the box.

Layout:
  engine/   MUNGA + MUNGA_L4 shared 2007 engine, carrying our BT render/loader
            work (bgfload/L4D3D/L4VIDEO: BSL bit-slice decode, LOD/ground/shadow
            models) + image codec; the minimal rp/ headers the audio HAL needs
  game/     reconstructed BT logic + surviving-original BT source + fwd shims
            + WinMain launcher
  content/  full runtime tree (BTL4.RES, VIDEO/, GAUGE/, AUDIO/, eggs, BTDPL.INI)
  docs/     format specs + reconstruction ledgers
  reference/ raw Ghidra pseudocode (recon source-of-truth) + decomp exporter
  tools/    MP console emulator + map/resource scanners

One top-level CMake builds munga_engine lib + bt410_l4 game lib + btl4.exe.
All paths relativized (186 fwd shims + ~437 CMake abs paths -> repo-relative);
DXSDK is the one external, overridable via -DDXSDK. Verified: builds to a
byte-identical 2.27MB exe and runs combat (TARGET DESTROYED, 0 crashes) against
the bundled content.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-05 21:03:40 -05:00

509 lines
18 KiB
C++

//===========================================================================//
// File: heat.hpp //
// Project: BattleTech Brick: Entity Manager //
// Contents: Heatable subsystems -- temperature model, heat sinks, condenser //
//---------------------------------------------------------------------------//
// Date Who Modification //
// -------- --- ---------------------------------------------------------- //
// --/--/95 ?? Initial coding. //
//---------------------------------------------------------------------------//
// Copyright (C) 1995, Virtual World Entertainment, Inc. All Rights reserved //
// PROPRIETARY AND CONFIDENTIAL //
//===========================================================================//
//
// RECONSTRUCTED from the shipped binary (Ghidra pseudo-C in heat_cluster.c)
// cross-referenced with the surviving HEAT.TCP fragment and the MUNGA
// SUBSYSTM.HPP base interface. See heat.cpp for per-method @ADDR evidence.
//
// NOTE on the engine base: the WinTesla/MUNGA `Subsystem` (subsystm.h) is a
// thin damageable-simulation base -- it does NOT carry the heat/power virtual
// surface (ResetToInitialState / GetStatusFlags / HandleMessage / PrintState /
// Simulation) that the BT game layer assumed lived on "Subsystem". Because
// this family OWNS the subsystem base classes that the other BT families
// derive from, that virtual surface is (re)introduced on HeatableSubsystem
// here, and the per-class shared-data boilerplate follows the real engine
// GetClassDerivations()/GetMessageHandlers()/GetAttributeIndex()/StateCount
// idiom (cf. RP/VTVSUB.cpp), not the 3-arg form the raw decomp guessed.
//
#if !defined(HEAT_HPP)
# define HEAT_HPP
#if !defined(SUBSYSTM_HPP)
# include <subsystm.hpp>
#endif
#if !defined(MECHSUB_HPP)
# include <mechsub.hpp> // MechSubsystem -- HeatableSubsystem's base (re-base)
#endif
#include <affnmtrx.hpp> // AffineMatrix
#include <average.hpp> // AverageOf<T>
#include <scalar.hpp> // Scalar
#include <alarm.hpp> // GaugeAlarm
#include <string.h>
#include <stdlib.h>
//##################### Reconstruction type aliases #######################
#if !defined(BT_RECON_TYPE_ALIASES)
# define BT_RECON_TYPE_ALIASES
typedef AffineMatrix Matrix34; // 3x4 affine (AFFNMTRX.h)
typedef GaugeAlarm AlarmIndicator; // GAUGALRM.h
typedef AverageOf<Scalar> FilteredScalar; // 15-sample running average (AVERAGE.h)
#endif
// NOTE: `DebugStream` and `endl` (the ReconStream trace artifact) come from the
// shared foundation header (mechrecon.hpp, pulled in via bt.hpp/mech.hpp).
//##################### Segment flag bits (model resource) ###############
// The "is this a master / damaged-copy segment" test. The decomp rendered
// these as owner->GetSegmentFlags() reads; the flags actually live in the
// streamed subsystem resource (subsystemFlags).
#if !defined(BT_SEGMENT_FLAG_BITS)
# define BT_SEGMENT_FLAG_BITS
enum {
SegmentCopyMask = 0x0C, // (flags & 0xC): 0 == master, 4 == copy
MasterHeatSinkFlag = 0x100 // flags & 0x100 == participates in sim
};
#endif
//##################### Forward Class Declarations #######################
class Mech;
//###########################################################################
//################# Reconstruction helper value types ###################
//###########################################################################
//
// The raw decomp leaned on three engine helpers whose real APIs differ from
// what the pseudo-C assumed. Rather than bend the engine types, the family
// owns small faithful wrappers that expose exactly the surface the recovered
// bodies call.
//
// A 3-/5-level degradation indicator (the original AlarmIndicator surface:
// SetLevel/GetLevel/Initialize). Backed by a GaugeAlarm in the shipped build.
class HeatAlarm
{
public:
HeatAlarm(int levels = 0) { levelCount = levels; level = 0; }
void Initialize(int levels) { levelCount = levels; level = 0; }
void SetLevel(int n) { level = n; }
int GetLevel() const { return level; }
int Level() const { return level; }
protected:
int levelCount;
int level;
};
// The 15-sample running average behind heatLoad (Initialize/AddSample/Average).
class HeatFilter
{
public:
void Initialize(int count, Scalar value) { average.SetSize((size_t)count, value); }
void AddSample(Scalar v) { average.Add(v); }
Scalar Average() { return average.CalculateAverage(); }
private:
AverageOf<Scalar> average;
};
// A ref to a linked subsystem (heat-sink linkage / voltage source / watched
// subsystem). The original modelled this with a small SharedData-derived
// connection object exposing Add/Resolve/Clear.
class SubsystemConnection
{
public:
SubsystemConnection(int = 0) { linked = 0; }
void Add(Subsystem *s) { linked = s; }
Subsystem* Resolve() const { return linked; }
void Clear() { linked = 0; }
protected:
Subsystem *linked;
};
// condenserNumber = atoi(lastChar(name)).
inline int
NameTrailingNumber(const char *name)
{
if (name == 0 || *name == '\0')
return 0;
return atoi(name + (strlen(name) - 1));
}
//###########################################################################
//################# HeatableSubsystem Model Resource ####################
//###########################################################################
//
// Extends the base damageable Subsystem resource. The heat-specific fields
// begin at +0xE4 -- i.e. immediately after MechSubsystem__SubsystemResource
// (model size 0xE4), NOT after Subsystem::SubsystemResource (0x30). The class
// hierarchy is HeatableSubsystem : MechSubsystem : Subsystem, so the resource
// MUST inherit MechSubsystem__SubsystemResource; inheriting Subsystem::
// SubsystemResource directly dropped the 0x30..0xE4 chunk and slid every heat
// field 0xB4 bytes low -- so thermalMass/heatSinkIndex read neighbouring floats
// (heatSinkIndex came back as 10.0f = 1/thermalMass, GetSegment -> OOB -> no link).
//
struct HeatableSubsystem__SubsystemResource:
public MechSubsystem__SubsystemResource
{
Scalar startingTemperature; // +0xE4 "StartingTemperature"
Scalar degradationTemperature; // +0xE8 "DegradationTemperature"
Scalar failureTemperature; // +0xEC "FailureTemperature"
Scalar thermalConductance; // +0xF0 "ThermalConductance"
Scalar thermalMass; // +0xF4 "ThermalMass"
int heatSinkIndex; // +0xF8 "HeatSink" (segment index, +2 bias)
};
//###########################################################################
//################# Condenser Model Resource ############################
//###########################################################################
//
// Condenser adds the refrigeration factor (+0xFC). Declared here because the
// Condenser class itself is declared in this header (its bodies are split
// between heat.cpp [best-effort] and heatfamily_reslice.cpp [completion]).
//
struct Condenser__SubsystemResource:
public HeatableSubsystem__SubsystemResource
{
Scalar refrigerationFactor; // +0xFC "RefrigerationFactor" (sentinel -1.0f)
};
//###########################################################################
//######################### HeatableSubsystem ###########################
//###########################################################################
//
// Abstract base for any subsystem that participates in the thermal model and
// the shared "damageable subsystem" virtual surface the BT families expect.
// (vtable @0050e210, destructor @004ac868.)
//
class HeatableSubsystem:
public MechSubsystem // was: public Subsystem. mechsub.hpp's MechSubsystem
// and this are overlapping reconstructions of the SAME
// binary cluster (vtable 0050e210); re-base + de-shadow so
// owner/simulationState/damageZone/the virtual surface come
// from the one real base, not uninitialised duplicates.
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Shared Data Support
//
public:
static Derivation *GetClassDerivations();
static SharedData DefaultData;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test Class Support
//
public:
static Logical
TestClass(Mech&);
Logical
TestInstance() const;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Damageable-subsystem virtual surface (engine Subsystem lacks these; the
// heat/power families override them).
//
public:
// These OVERRIDE the MechSubsystem base slots (same vtable, matching sigs) --
// not new/parallel slots. ResetToInitialState takes (Logical powered) to match.
virtual void
ResetToInitialState(Logical powered);
virtual LWord
GetStatusFlags();
virtual Logical
HandleMessage(int message);
virtual void
PrintState();
virtual void
Simulation(Scalar time_slice); // heat per-frame (not a MechSubsystem slot)
Logical
IsDamaged() { return (simulationState != 0) ? True : False; }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Construction and Destruction
//
public:
typedef HeatableSubsystem__SubsystemResource SubsystemResource;
HeatableSubsystem(
Mech *owner,
int subsystem_ID,
SubsystemResource *subsystem_resource,
SharedData &shared_data = DefaultData
);
~HeatableSubsystem();
static int
CreateStreamedSubsystem(
NotationFile *model_file,
const char *model_name,
const char *subsystem_name,
SubsystemResource *subsystem_resource,
NotationFile *subsystem_file,
const ResourceDirectories *directories,
int passes = 1
);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Common subsystem state
//
public:
// owner / simulationState(was "destroyed") / damageZone / hostEntity etc. are
// INHERITED from MechSubsystem -- re-declaring them shadowed the real base
// (engine ctor writes the base member; the duplicate is uninitialised + at a
// wrong compiled offset). flags/statusFlags -> engine Simulation::simulationFlags;
// statusBits -> ForceUpdate()/dirty word. Only the thermal fields are own.
Scalar currentTemperature; // @0x114 HEAT.TCP: init 300.0f
Scalar degradationTemperature; // @0x118
Scalar failureTemperature; // @0x11C
Scalar heatLoad; // @0x120 HEAT.TCP: init 0.0f (filtered load)
};
//###########################################################################
//############################# HeatSink ################################
//###########################################################################
//
// Active heat sink. Accumulates heat energy, conducts heat to a linked
// sink, draws coolant, and raises a degradation/failure alarm.
// (vtable @0050edc4, ctor @004adda0, dtor @004adfd4.)
//
class HeatSink:
public HeatableSubsystem
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Shared Data Support
//
public:
static Derivation *GetClassDerivations();
static SharedData DefaultData;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Heat alarm state (this+0x138, reported by PrintState @004ae050)
//
public:
enum HeatState {
NormalHeat = 0,
DegradationHeat = 1,
FailureHeat = 2
};
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Simulation Support
//
public:
typedef void
(HeatSink::*Performance)(Scalar time_slice);
void
SetPerformance(Performance performance)
{
Check(this);
activePerformance = (Simulation::Performance)performance;
}
void
HeatSinkSimulation(Scalar time_slice); // @004ad924 (Performance)
// FUN_004ad748 -- the base per-frame step (vtable slot 9), reused by the
// Condenser / aggregate sink in heatfamily_reslice.cpp.
void
HeatSink_Step(Scalar time_slice) { HeatSinkSimulation(time_slice); }
// FUN_004ad7d4 -- entity heat-model active flag.
Logical
HeatModelActive() { return True; }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Subsystem virtual overrides (slots on vtable @0050edc4)
//
public:
LWord
GetStatusFlags(); // slot 12, @004add30
Logical
HandleMessage(int message); // slot 8, @004add6c
void
ResetToInitialState(Logical powered); // slot 10, @004ad760
void
PrintState(); // slot 13, @004ae050
// slot 14 (vtable+0x38): asks the central cooling system for coolant
// and returns how much was actually supplied (overridden by Reservoir).
virtual Scalar
DrawCoolant(Scalar requested);
// FUN: link another sink/reservoir into this one (Reservoir ctor).
void
Attach(HeatSink *other) { linkedSinks.Add(other); }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test Class Support
//
public:
static Logical
TestClass(Mech&);
Logical
TestInstance() const;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Construction and Destruction
//
public:
typedef HeatableSubsystem__SubsystemResource SubsystemResource;
HeatSink(
Mech *owner,
int subsystem_ID,
SubsystemResource *subsystem_resource,
SharedData &shared_data = DefaultData
);
~HeatSink();
static int
CreateStreamedSubsystem(
NotationFile *model_file,
const char *model_name,
const char *subsystem_name,
SubsystemResource *subsystem_resource,
NotationFile *subsystem_file,
const ResourceDirectories *directories,
int passes = 1
);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Internal model helpers
//
public:
void
UpdateHeatLoad(); // @004ad7f0
void
ClearHeatFilter(); // @004ad884
void
ConductHeat(Scalar time_slice); // @004ad8ac
Scalar
ComputeHeatFlow( // @004ad9ec
HeatSink *other,
Scalar time_slice
);
void
BalanceCoolant( // @004ada94
Scalar time_slice
);
void
UpdateCoolant(Scalar time_slice); // @004adbf8
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Local data for the heat sink class.
// Offsets are byte offsets into the shipped object.
//
public:
// --- temperature thresholds (read from resource at +0xE8/+0xEC) ---
Scalar degradationTemperature; // @0x118 resource +0xE8
Scalar failureTemperature; // @0x11C resource +0xEC
// --- coolant model ---
Scalar coolantEfficiency; // @0x124 init 0.5f
Scalar thermalCapacity; // @0x128 init 1.0f (coolant capacity / divisor)
Scalar coolantLevel; // @0x12C init = thermalCapacity (1.0f)
Scalar coolantDraw; // @0x130 init 0 (per-frame coolant demand)
int coolantAvailable; // @0x134 init 1 (coolant supply present)
int coolantActive; // @0x138 init 0 (draw state machine)
int heatState; // @0x184 heat-state code (Normal/Degradation/Failure)
LWord heatModelFlag; // @0x138 heat-model running flag
// --- thermal parameters ---
Scalar startingTemperature; // @0x13C resource +0xE4 (saved initial temp)
Scalar thermalConductance; // @0x140 resource +0xF0
HeatFilter heatFilter; // @0x144 15-sample running average
Scalar filterDecay; // @0x150 init 0.4f
Scalar thermalMass; // @0x154 resource +0xF4
Scalar heatEnergy; // @0x158 init = thermalMass * startingTemperature
Scalar coolantFlowScale; // @0x15C init 1.0f (== "word57")
Scalar massScale; // @0x160 init 1.0f
// --- linkage / display ---
SubsystemConnection linkedSinks; // @0x164 connection to master/linked heat sink
HeatAlarm heatAlarm; // @0x170 3-level alarm (Normal/Degradation/Failure)
SubsystemResource *resource; // @0x1C4 saved resource pointer
Scalar pendingHeat; // @0x1C8 init 0 (heat delta queued for next frame)
Scalar radiatedHeat; // @0x1CC currentTemperature * coolantLevel
Scalar field_1d0; // @0x1D0 (master-sink scaling field; best-effort)
};
//###########################################################################
//############################# Condenser ###############################
//###########################################################################
//
// A HeatSink subclass that models a refrigeration output fighting the master
// heat sink's stored heat, carries a 3-position valve and a condenser number.
// (vtable @0050ed88, ctor @4ae568, classID CondenserClassID.) The full body
// set lives in heatfamily_reslice.cpp; heat.cpp carries a best-effort stub.
//
class Condenser:
public HeatSink
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Shared Data Support
//
public:
static Derivation *GetClassDerivations();
static SharedData DefaultData;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test Class Support
//
public:
static Logical
TestClass(Mech&);
Logical
TestInstance() const;
void
ResetToInitialState(Logical powered);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Simulation / overrides
//
public:
void
RefrigerationSimulation(Scalar time_slice); // @4ae4d8 (vtable slot 9)
void
MoveValve(int message); // @4afbe0 (best-effort)
virtual void
SetValveSetting(int setting) { valveState = setting; }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Construction and Destruction
//
public:
typedef Condenser__SubsystemResource SubsystemResource;
Condenser(
Mech *owner,
int subsystem_ID,
SubsystemResource *subsystem_resource,
SharedData &shared_data = DefaultData
);
~Condenser();
static int
CreateStreamedSubsystem(
NotationFile *model_file,
const char *model_name,
const char *subsystem_name,
SubsystemResource *subsystem_resource,
NotationFile *subsystem_file,
const ResourceDirectories *directories,
int passes = 1
);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Local data.
//
public:
Scalar refrigerationOutput; // @0x160 (word 0x58) recomputed each frame
int valveState; // @0x1D0 (word 0x74) init 1 (MoveValve 0..2)
int condenserNumber; // @0x1D4 (word 0x75) last digit of name
Scalar refrigerationFactor; // @0x1D8 (word 0x76) from resource +0xFC
HeatAlarm condenserAlarm; // @0x1DC (word 0x77) 3 levels
};
#endif