Skip to content

Commit

Permalink
Weapon Randomizer: Add Weapon Models Randomizer
Browse files Browse the repository at this point in the history
* Pretty unstable; Had to fix one crash related to projectile
  throwing. Requires a grouped data file (WeaponModels.txt). New
  Randomizer for use with Parser (and also not).
  • Loading branch information
Parik27 committed May 26, 2021
1 parent 328d8bd commit 607d3fe
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 48 deletions.
81 changes: 81 additions & 0 deletions src/common/parser.hh
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#pragma once

#include "ParserUtils.hh"
#include "common/common.hh"
#include <cstdint>
#include <map>
#include <string.h>
#include <type_traits>
#include <tuple>
#include <array>
#include <utility>

/* A randomizer to randomize a field between its min/max value */
template <typename T> class RangedRandomizer
Expand Down Expand Up @@ -35,6 +38,84 @@ public:
}
};

template <const char *FileName, auto ValidateFunction = nullptr>
class DataFileBasedModelRandomizer
{
using Type = uint32_t;

private:
bool m_Initialised = false;
std::vector<std::vector<Type>> m_Values;

/*******************************************************/
void
Initialise ()
{
if (std::exchange (m_Initialised, true))
return;

FILE *file = Rainbomizer::Common::GetRainbomizerDataFile (FileName);
if (!file)
return;

m_Values.push_back ({});

char line[512] = {0};
while (fgets (line, 512, file))
{
if (strlen (line) < 3)
{
m_Values.push_back ({});
continue;
}

line[strcspn (line, "\n")] = 0;

Type value = rage::atStringHash (line);
if (!ValidateFunction || ValidateFunction (value))
m_Values.back ().push_back (value);
}
}

public:
/*******************************************************/
void
RandomizeObject (Type &out)
{
Initialise ();
for (const auto &i : m_Values)
{
if (DoesElementExist (i, out))
out = GetRandomElement (i);
}
}

/*******************************************************/
void
AddSample (const Type value)
{
}
};

template <typename T, T... Values> class ConstantValues
{
constexpr static std::array<T, sizeof...(Values)> m_Values{Values...};

public:
using Type = T;

void
RandomizeObject (T &out) const
{
out = GetRandomElement (m_Values);
}

void
AddSample (const T value)
{
}
};

/* A randomizer to shuffle a field's possible values */
template <typename T> class ShuffleRandomizer
{
Expand Down
5 changes: 4 additions & 1 deletion src/peds/peds_SkellyFixes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PedRandomizerSkeletonFixes
Fix_crSkeleton_SetGlobalMtx (crSkeleton *skelly, uint32_t id,
rage::Mat34V *mat)
{
if (id == -1)
if (id == -1 || !skelly)
return;

crSkeleton_SetGlobalMtx (skelly, id, mat);
Expand All @@ -60,6 +60,9 @@ class PedRandomizerSkeletonFixes
Fix_crSkeleton_GetGlobalMtx (crSkeleton *skelly, uint32_t id,
rage::Mat34V *mat)
{
if (!skelly)
return;

if (id == -1)
id = 0;

Expand Down
63 changes: 21 additions & 42 deletions src/weapons/stats.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "CItemInfo.hh"
#include "CStreaming.hh"
#include "Utils.hh"
#include "common/common.hh"
#include "common/config.hh"
#include "common/parser.hh"
#include "common/logger.hh"
#include <cstdint>
#include <utility>

Expand All @@ -20,18 +22,8 @@ using CWeaponInfoRandomizerBase = ParserRandomHelper<
CWeaponInfo,
RandomizedFieldsWrapper<
ShuffleRandomizer<uint32_t>, "ClipSize"_joaat, "Audio"_joaat,
"BulletsInBatch"_joaat, "InitialRumbleDuration"_joaat,
"RumbleDuration"_joaat, "InitialRumbleDurationFps"_joaat,
"DefaultCameraHash"_joaat, "CoverCameraHash"_joaat,
"RunAndGunCameraHash"_joaat, "CinematicShootingCameraHash"_joaat,
"RecoilShakeHash"_joaat, "RecoilShakeHashFirstPerson"_joaat,
"MinTimeBetweenRecoilShakes"_joaat, "HumanNameHash"_joaat>,

RandomizedFieldsWrapper<
SelectiveRandomizer<ShuffleRandomizer<uint32_t>,
SelectionType::EXCLUDING, 0, 0xFFFFFFFF>,
"Model"_joaat>,

RandomizedFieldsWrapper<
ShuffleRandomizer<float>, "AccuracySpread"_joaat,
"AccurateModeAccuracyModifier"_joaat, "RunAndGunAccuracyModifier"_joaat,
Expand All @@ -44,50 +36,28 @@ using CWeaponInfoRandomizerBase = ParserRandomHelper<
"HeadShotDamageModifierPlayer"_joaat, "Damage"_joaat,
"DamageTime"_joaat, "DamageTimeInVehicle"_joaat,
"DamageTimeInVehicleHeadShot"_joaat, "HitLimbsDamageModifier"_joaat,
"NetworkHitLimbsDamageModifier"_joaat,
"LightlyArmouredDamageModifier"_joaat, "Force"_joaat,
"ForceHitPed"_joaat, "ForceHitVehicle"_joaat,
"ForceHitFlyingHeli"_joaat, "ForceMaxStrengthMult"_joaat,
"ForceFalloffRangeStart"_joaat, "ForceFalloffRangeEnd"_joaat,
"ForceFalloffMin"_joaat, "ProjectileForce"_joaat, "FragImpulse"_joaat,
"Penetration"_joaat, "VerticalLaunchAdjustment"_joaat,
"DropForwardVelocity"_joaat, "Speed"_joaat, "BatchSpread"_joaat,
"ReloadTimeMP"_joaat, "ReloadTimeSP"_joaat, "VehicleReloadTime"_joaat,
"AnimReloadRate"_joaat, "TimeBetweenShots"_joaat,
"TimeLeftBetweenShotsWhereShouldFireIsCached"_joaat, "SpinUpTime"_joaat,
"SpinTime"_joaat, "SpinDownTime"_joaat, "AlternateWaitTime"_joaat,
"BulletBendingNearRadius"_joaat, "BulletBendingFarRadius"_joaat,
"BulletBendingZoomedRadius"_joaat,
"ForceFalloffMin"_joaat, "Penetration"_joaat,
"VerticalLaunchAdjustment"_joaat, "DropForwardVelocity"_joaat,
"Speed"_joaat, "BatchSpread"_joaat, "BulletBendingNearRadius"_joaat,
"BulletBendingFarRadius"_joaat, "BulletBendingZoomedRadius"_joaat,
"FirstPersonBulletBendingFarRadius"_joaat,
"FirstPersonBulletBendingZoomedRadius"_joaat,
"InitialRumbleIntensity"_joaat, "InitialRumbleIntensityTrigger"_joaat,
"RumbleIntensity"_joaat, "RumbleIntensityTrigger"_joaat,
"RumbleDamageIntensity"_joaat, "InitialRumbleIntensityFps"_joaat,
"RumbleIntensityFps"_joaat, "NetworkPlayerDamageModifier"_joaat,
"NetworkPedDamageModifier"_joaat,
"NetworkHeadShotPlayerDamageModifier"_joaat, "LockOnRange"_joaat,
"WeaponRange"_joaat, "BulletDirectionOffsetInDegrees"_joaat,
"AiSoundRange"_joaat, "AiPotentialBlastEventRange"_joaat,
"DamageFallOffRangeMin"_joaat, "DamageFallOffRangeMax"_joaat,
"DamageFallOffModifier"_joaat, "CameraFov"_joaat,
"FirstPersonScopeFov"_joaat,
"FirstPersonBulletBendingZoomedRadius"_joaat, "WeaponRange"_joaat,
"BulletDirectionOffsetInDegrees"_joaat, "AiSoundRange"_joaat,
"AiPotentialBlastEventRange"_joaat, "DamageFallOffRangeMin"_joaat,
"DamageFallOffRangeMax"_joaat, "DamageFallOffModifier"_joaat,
"CameraFov"_joaat, "FirstPersonScopeFov"_joaat,
"FirstPersonDofSubjectMagnificationPowerFactorNear"_joaat,
"FirstPersonDofMaxNearInFocusDistance"_joaat,
"FirstPersonDofMaxNearInFocusDistanceBlendLevel"_joaat,
"ZoomFactorForAccurateMode"_joaat, "RecoilShakeAmplitude"_joaat,
"ExplosionShakeAmplitude"_joaat, "IkRecoilDisplacement"_joaat,
"IkRecoilDisplacementScope"_joaat,
"IkRecoilDisplacementScaleBackward"_joaat,
"IkRecoilDisplacementScaleVertical"_joaat, "AimProbeLengthMin"_joaat,
"AimProbeLengthMax"_joaat, "KillshotImpulseScale"_joaat,
"AimingBreathingAdditiveWeight"_joaat,
"FiringBreathingAdditiveWeight"_joaat,
"StealthAimingBreathingAdditiveWeight"_joaat,
"StealthFiringBreathingAdditiveWeight"_joaat,
"AimingLeanAdditiveWeight"_joaat, "FiringLeanAdditiveWeight"_joaat,
"StealthAimingLeanAdditiveWeight"_joaat,
"StealthFiringLeanAdditiveWeight"_joaat,
"ExpandPedCapsuleRadius"_joaat>>;
"IkRecoilDisplacementScaleVertical"_joaat>>;

using CWeaponInfoRandomizer
= ParserRandomHelperContainerForEachFieldValue<CWeaponInfoRandomizerBase,
Expand All @@ -100,7 +70,13 @@ using ItemInfoRandomizer

class WeaponStatsRandomizer
{
inline static char ModelFileName[] = "WeaponModels.txt";
using ModelsRandomizer
= DataFileBasedModelRandomizer<ModelFileName,
CStreaming::GetModelByHash<>>;

inline static ItemInfoRandomizer sm_ItemInfoRandomizer;
inline static ModelsRandomizer sm_WeaponModelRandomizer;

public:
/*******************************************************/
Expand All @@ -109,6 +85,9 @@ class WeaponStatsRandomizer
{
for (CItemInfo *i : CWeaponInfoManager::sm_Instance->aItemInfos)
{
if (i->Model != -1 && i->Model)
sm_WeaponModelRandomizer.RandomizeObject (i->Model);

std::uint32_t hash;
if (sample)
sm_ItemInfoRandomizer.AddSample (i, i->GetClassId (hash));
Expand Down
31 changes: 26 additions & 5 deletions src/weapons/weapons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,22 @@ class WeaponRandomizer
}
}
}
fclose (file);

double base_probability = 1;
double base_probability = 0.01;

std::vector<double> weights;
for (auto i : mValidWeapons)
{
double weight = base_probability;

// Multiply groupWeight with the current weight
if (auto *gW = LookupMap (probabilities, mValidWeaponGroups[i]))
weight = (*gW);

if (auto *weapWeight = LookupMap (probabilities, i))
weight = *weapWeight;

// Multiply groupWeight with the current weight
if (auto *gW = LookupMap (probabilities, mValidWeaponGroups[i]))
weight *= (*gW);

weights.push_back (weight);
}

Expand Down Expand Up @@ -131,6 +132,7 @@ class WeaponRandomizer
}

InitialiseWeaponWeights ();
PrintWeaponList ();
Rainbomizer::Logger::LogMessage ("Initialised %d valid weapons",
mValidWeapons.size ());
}
Expand Down Expand Up @@ -212,6 +214,25 @@ class WeaponRandomizer
true);
}

/*******************************************************/
static void
PrintWeaponList ()
{
for (auto &info : CWeaponInfoManager::sm_Instance->aItemInfos)
{
uint32_t outHash = 0;
bool valid
= info->Model
&& info->GetClassId (outHash) == "cweaponinfo"_joaat
&& IsValidWeapon (*static_cast<CWeaponInfo *> (info));

Rainbomizer::Logger::LogMessage (
"classId => %x, modelId = %x, name = %x, value = %s",
info->GetClassId (outHash), info->Model, info->Name,
valid ? "true" : "false");
}
}

/*******************************************************/
inline static void *
GetGiveWeaponFuncAddress ()
Expand Down

0 comments on commit 607d3fe

Please sign in to comment.