feat: add T4 PhysPreset support (#925)

* feat: add T4 PhysPreset support

* chore: slight adjustment to how the max friction constant is named

---------

Co-authored-by: Jan Laupetin <[email protected]>
This commit is contained in:
mo
2026-07-21 23:56:59 +02:00
committed by GitHub
co-authored by Jan Laupetin
parent 572965844e
commit 37bb67a3a5
14 changed files with 490 additions and 1 deletions
+4
View File
@@ -11,6 +11,8 @@
#include "Localize/AssetLoaderLocalizeT4.h"
#include "Maps/MapEntsLoaderT4.h"
#include "Material/LoaderMaterialT4.h"
#include "PhysPreset/GdtLoaderPhysPresetT4.h"
#include "PhysPreset/RawLoaderPhysPresetT4.h"
#include "RawFile/AssetLoaderRawFileT4.h"
#include "Weapon/FlameTableLoaderT4.h"
#include "Weapon/WeaponGdtLoaderT4.h"
@@ -103,6 +105,8 @@ namespace
collection.AddAssetCreator(font::CreateLoaderT4(memory, searchPath));
collection.AddAssetCreator(localize::CreateLoaderT4(memory, searchPath, zone));
collection.AddAssetCreator(map_ents::CreateLoaderT4(memory, searchPath));
collection.AddAssetCreator(phys_preset::CreateRawLoaderT4(memory, searchPath, zone));
collection.AddAssetCreator(phys_preset::CreateGdtLoaderT4(memory, gdt, zone));
collection.AddAssetCreator(raw_file::CreateLoaderT4(memory, searchPath));
collection.AddAssetCreator(weapon::CreateRawLoaderT4(memory, searchPath, zone));
collection.AddAssetCreator(weapon::CreateGdtLoaderT4(memory, searchPath, gdt, zone));
@@ -0,0 +1,50 @@
#include "GdtLoaderPhysPresetT4.h"
#include "Game/T4/ObjConstantsT4.h"
#include "Game/T4/T4.h"
#include "InfoString/InfoString.h"
#include "InfoStringLoaderPhysPresetT4.h"
#include "Utils/Logging/Log.h"
using namespace T4;
namespace
{
class GdtLoaderPhysPreset final : public AssetCreator<AssetPhysPreset>
{
public:
GdtLoaderPhysPreset(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone)
: m_gdt(gdt),
m_info_string_loader(memory, zone)
{
}
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
{
const auto* gdtEntry = m_gdt.GetGdtEntryByGdfAndName(GDF_FILENAME_PHYS_PRESET, assetName);
if (gdtEntry == nullptr)
return AssetCreationResult::NoAction();
InfoString infoString;
if (!infoString.FromGdtProperties(*gdtEntry))
{
con::error("Failed to read phys preset gdt entry: \"{}\"", assetName);
return AssetCreationResult::Failure();
}
return m_info_string_loader.CreateAsset(assetName, infoString, context);
}
private:
IGdtQueryable& m_gdt;
phys_preset::InfoStringLoaderT4 m_info_string_loader;
};
} // namespace
namespace phys_preset
{
std::unique_ptr<AssetCreator<AssetPhysPreset>> CreateGdtLoaderT4(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone)
{
return std::make_unique<GdtLoaderPhysPreset>(memory, gdt, zone);
}
} // namespace phys_preset
@@ -0,0 +1,14 @@
#pragma once
#include "Asset/IAssetCreator.h"
#include "Game/T4/T4.h"
#include "Gdt/IGdtQueryable.h"
#include "SearchPath/ISearchPath.h"
#include "Utils/MemoryManager.h"
#include <memory>
namespace phys_preset
{
std::unique_ptr<AssetCreator<T4::AssetPhysPreset>> CreateGdtLoaderT4(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone);
} // namespace phys_preset
@@ -0,0 +1,94 @@
#include "InfoStringLoaderPhysPresetT4.h"
#include "Game/T4/InfoString/InfoStringToStructConverter.h"
#include "Game/T4/ObjConstantsT4.h"
#include "Game/T4/PhysPreset/PhysPresetFields.h"
#include "Game/T4/T4.h"
#include "Utils/Logging/Log.h"
#include <algorithm>
#include <cassert>
#include <type_traits>
using namespace T4;
namespace
{
class InfoStringToPhysPresetConverter final : public InfoStringToStructConverter
{
protected:
bool ConvertExtensionField(const cspField_t& field, const std::string& value) override
{
assert(false);
return false;
}
public:
InfoStringToPhysPresetConverter(const InfoString& infoString,
PhysPresetInfo& physPreset,
ZoneScriptStrings& zoneScriptStrings,
MemoryManager& memory,
AssetCreationContext& context,
AssetRegistration<AssetPhysPreset>& registration,
const cspField_t* fields,
const size_t fieldCount)
: InfoStringToStructConverter(infoString, &physPreset, zoneScriptStrings, memory, context, registration, fields, fieldCount)
{
}
};
void CopyFromPhysPresetInfo(const PhysPresetInfo& physPresetInfo, PhysPreset& physPreset)
{
physPreset.mass = std::clamp(physPresetInfo.mass, 1.0f, 2000.0f) * 0.001f;
physPreset.bounce = physPresetInfo.bounce;
if (physPresetInfo.isFrictionInfinity != 0)
physPreset.friction = PHYS_PRESET_MAX_FRICTION;
else
physPreset.friction = physPresetInfo.friction;
physPreset.bulletForceScale = physPresetInfo.bulletForceScale;
physPreset.explosiveForceScale = physPresetInfo.explosiveForceScale;
physPreset.sndAliasPrefix = physPresetInfo.sndAliasPrefix;
physPreset.piecesSpreadFraction = physPresetInfo.piecesSpreadFraction;
physPreset.piecesUpwardVelocity = physPresetInfo.piecesUpwardVelocity;
physPreset.canFloat = physPresetInfo.canFloat;
physPreset.gravityScale = std::clamp(physPresetInfo.gravityScale, 0.01f, 10.0f);
}
} // namespace
namespace phys_preset
{
InfoStringLoaderT4::InfoStringLoaderT4(MemoryManager& memory, Zone& zone)
: m_memory(memory),
m_zone(zone)
{
}
AssetCreationResult InfoStringLoaderT4::CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context)
{
auto* physPreset = m_memory.Alloc<PhysPreset>();
physPreset->name = m_memory.Dup(assetName.c_str());
AssetRegistration<AssetPhysPreset> registration(assetName, physPreset);
PhysPresetInfo physPresetInfo{};
InfoStringToPhysPresetConverter converter(infoString,
physPresetInfo,
m_zone.m_script_strings,
m_memory,
context,
registration,
phys_preset_fields,
std::extent_v<decltype(phys_preset_fields)>);
if (!converter.Convert())
{
con::error("Failed to parse phys preset: \"{}\"", assetName);
return AssetCreationResult::Failure();
}
CopyFromPhysPresetInfo(physPresetInfo, *physPreset);
return AssetCreationResult::Success(context.AddAsset(std::move(registration)));
}
} // namespace phys_preset
@@ -0,0 +1,20 @@
#pragma once
#include "Asset/AssetCreationContext.h"
#include "Asset/AssetCreationResult.h"
#include "InfoString/InfoString.h"
namespace phys_preset
{
class InfoStringLoaderT4
{
public:
InfoStringLoaderT4(MemoryManager& memory, Zone& zone);
AssetCreationResult CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context);
private:
MemoryManager& m_memory;
Zone& m_zone;
};
} // namespace phys_preset
@@ -0,0 +1,52 @@
#include "RawLoaderPhysPresetT4.h"
#include "Game/T4/ObjConstantsT4.h"
#include "Game/T4/T4.h"
#include "InfoString/InfoString.h"
#include "InfoStringLoaderPhysPresetT4.h"
#include "PhysPreset/PhysPresetCommon.h"
#include "Utils/Logging/Log.h"
using namespace T4;
namespace
{
class RawLoaderPhysPreset final : public AssetCreator<AssetPhysPreset>
{
public:
RawLoaderPhysPreset(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
: m_search_path(searchPath),
m_info_string_loader(memory, zone)
{
}
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
{
const auto fileName = phys_preset::GetFileNameForAssetName(assetName);
const auto file = m_search_path.Open(fileName);
if (!file.IsOpen())
return AssetCreationResult::NoAction();
InfoString infoString;
if (!infoString.FromStream(INFO_STRING_PREFIX_PHYS_PRESET, *file.m_stream))
{
con::error("Could not parse as info string file: \"{}\"", fileName);
return AssetCreationResult::Failure();
}
return m_info_string_loader.CreateAsset(assetName, infoString, context);
}
private:
ISearchPath& m_search_path;
phys_preset::InfoStringLoaderT4 m_info_string_loader;
};
} // namespace
namespace phys_preset
{
std::unique_ptr<AssetCreator<AssetPhysPreset>> CreateRawLoaderT4(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
{
return std::make_unique<RawLoaderPhysPreset>(memory, searchPath, zone);
}
} // namespace phys_preset
@@ -0,0 +1,13 @@
#pragma once
#include "Asset/IAssetCreator.h"
#include "Game/T4/T4.h"
#include "SearchPath/ISearchPath.h"
#include "Utils/MemoryManager.h"
#include <memory>
namespace phys_preset
{
std::unique_ptr<AssetCreator<T4::AssetPhysPreset>> CreateRawLoaderT4(MemoryManager& memory, ISearchPath& searchPath, Zone& zone);
} // namespace phys_preset