Files
BT411/engine/MUNGA/AVERAGE.h
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

299 lines
6.4 KiB
C++

#pragma once
#include "style.h"
//##########################################################################
//########################### AverageOf ##############################
//##########################################################################
template <class T> class AverageOf SIGNATURED
{
public:
//
//-----------------------------------------------------------------------
// Constructor, Destructor, Testing
//-----------------------------------------------------------------------
//
AverageOf();
AverageOf(
size_t size,
T initial=(T)0
);
~AverageOf();
Logical
TestInstance() const;
//
//-----------------------------------------------------------------------
// SetSize
//-----------------------------------------------------------------------
//
void
SetSize(
size_t size,
T initial=(T)0
);
//
//-----------------------------------------------------------------------
// Add
//-----------------------------------------------------------------------
//
void
Add(T value);
//
//-----------------------------------------------------------------------
// CalculateAverage
//-----------------------------------------------------------------------
//
T
CalculateAverage();
//
//-----------------------------------------------------------------------
// CalculateOlympicAverage
//-----------------------------------------------------------------------
//
T
CalculateOlympicAverage();
//
//-----------------------------------------------------------------------
// Calculate Trend of Average
//-----------------------------------------------------------------------
//
Scalar
CalculateTrend();
private:
//
//-----------------------------------------------------------------------
// Private data
//-----------------------------------------------------------------------
//
size_t size;
size_t next;
T *array;
};
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ AverageOf templates ~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T>
AverageOf<T>::AverageOf()
{
array = NULL;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T>
AverageOf<T>::AverageOf(
size_t the_size,
T initial
)
{
array = NULL;
SetSize(the_size, initial);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> void
AverageOf<T>::SetSize(
size_t the_size,
T initial
)
{
if (array != NULL)
{
Unregister_Pointer(array);
delete[] array;
}
size = the_size;
array = new T[size];
Register_Pointer(array);
next = 0;
for (size_t i = 0; i < size; i++)
{
Check_Pointer(array);
Verify(i < size);
array[i] = initial;
Check_Fpu();
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T>
AverageOf<T>::~AverageOf()
{
Unregister_Pointer(array);
delete[] array;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> Logical
AverageOf<T>::TestInstance() const
{
Check_Pointer(array);
Verify(next < size);
return True;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> void
AverageOf<T>::Add(T value)
{
Check(this);
Check_Pointer(array);
Verify(next < size);
array[next] = value;
++next;
if (next >= size)
{
Verify(next == size);
next = 0;
}
Check_Fpu();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> T
AverageOf<T>::CalculateAverage()
{
Check(this);
size_t i;
T accumulate;
for (i = 0, accumulate = (T)0; i < size; i++)
{
Check_Pointer(array);
Verify(i < size);
accumulate += array[i];
Check_Fpu();
}
Verify(!Small_Enough((T)size));
return (accumulate / (T)size);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> T
AverageOf<T>::CalculateOlympicAverage()
{
Check(this);
size_t i;
T accumulate, min_value, max_value;
Verify(0 < size);
min_value = array[0];
max_value = array[0];
Check_Fpu();
for (i = 0, accumulate = (T)0; i < size; i++)
{
Check_Pointer(array);
Verify(i < size);
accumulate += array[i];
Check_Fpu();
min_value = Min(array[i], min_value);
max_value = Max(array[i], max_value);
Check_Fpu();
}
accumulate -= min_value;
accumulate -= max_value;
Check_Fpu();
Verify(!Small_Enough((T)(size - 2)));
return (accumulate / (T)(size - 2));
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> Scalar
AverageOf<T>::CalculateTrend()
{
Check(this);
size_t i;
Scalar f = 0.0f;
Scalar fx = 0.0f;
Scalar x1 = size;
Scalar x2 = x1*x1/2.0f;
Scalar x3 = x2 + x1*x1*x1/3.0f + x1/6.0f;
x2 += x1/2.0f;
i = next;
Scalar t = 1.0f;
do
{
f += array[i];
fx += t*array[i];
t += 1.0f;
if (++i == size)
{
i = 0;
}
} while (i != next);
return (x1*fx - x2*f) / (x1*x3 - x2*x2);
}
//##########################################################################
//######################## StaticAverageOf ###########################
//##########################################################################
template <class T> class StaticAverageOf SIGNATURED
{
public:
//
//-----------------------------------------------------------------------
// Constructor, Destructor, Testing
//-----------------------------------------------------------------------
//
StaticAverageOf()
{size = (T)0; total = (T)0;}
~StaticAverageOf()
{}
Logical
TestInstance() const
{return True;}
//
//-----------------------------------------------------------------------
// Add
//-----------------------------------------------------------------------
//
void
Add(T value)
{Check(this); size++; total += value;}
//
//-----------------------------------------------------------------------
// CalculateAverage
//-----------------------------------------------------------------------
//
T
CalculateAverage()
{Check(this); return (size == 0) ? ((T)0) : (total / (T)size);}
//
//-----------------------------------------------------------------------
// GetSize
//-----------------------------------------------------------------------
//
size_t
GetSize()
{Check(this); return size;}
private:
//
//-----------------------------------------------------------------------
// Private data
//-----------------------------------------------------------------------
//
size_t size;
T total;
};