Skip to content

Commit

Permalink
Mission Randomizer: Fix family3 softlock when starting as Trevor
Browse files Browse the repository at this point in the history
  • Loading branch information
Parik27 committed Jun 2, 2021
1 parent 3a8d548 commit 0e6da15
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build-count.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
392
395
9 changes: 9 additions & 0 deletions lib/Utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ RandomInt (int min, int max)
return dist (RandEngine ());
}

/*******************************************************/
unsigned int
RandomWeighed (const std::vector<double> &weights)
{
std::discrete_distribution<unsigned int> dist{weights.begin (),
weights.end ()};
return dist (RandEngine ());
}

/*******************************************************/
int
RandomInt (int max)
Expand Down
12 changes: 7 additions & 5 deletions lib/Utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ ConvertCall (Addr address, Func &func)
func = Func (address);
}

int RandomInt (int max);
int RandomInt (int min, int max);
size_t RandomSize (size_t max);
float RandomFloat (float min, float max);
float RandomFloat (float max);
int RandomInt (int max);
int RandomInt (int min, int max);
size_t RandomSize (size_t max);
float RandomFloat (float min, float max);
float RandomFloat (float max);
unsigned int RandomWeighed (const std::vector<double> &weights);

void InitialiseAllComponents ();

/*******************************************************/
Expand Down
40 changes: 30 additions & 10 deletions src/common/parser.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include "ParserUtils.hh"
#include "common/common.hh"
#include "common/logger.hh"
#include <cstdint>
#include <map>
#include <stdio.h>
#include <string.h>
#include <type_traits>
#include <tuple>
Expand Down Expand Up @@ -44,8 +46,14 @@ class DataFileBasedModelRandomizer
using Type = uint32_t;

private:
bool m_Initialised = false;
std::vector<std::vector<Type>> m_Values;
struct ValueGroup
{
std::vector<uint32_t> Values;
std::vector<double> Weights;
};

bool m_Initialised = false;
std::vector<ValueGroup> m_Groups;

/*******************************************************/
void
Expand All @@ -58,22 +66,31 @@ private:
if (!file)
return;

m_Values.push_back ({});
m_Groups.push_back ({});

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

line[strcspn (line, "\n")] = 0;
double weight = 1.0;
char model[256] = {0};

Type value = rage::atStringHash (line);
sscanf (line, "%s %lf", model, &weight);
if (fabs (weight - 1.0) > 0.1)
Rainbomizer::Logger::LogMessage ("%s => %lf", model,
weight);

Type value = rage::atStringHash (model);
if (!ValidateFunction || ValidateFunction (value))
m_Values.back ().push_back (value);
{
m_Groups.back ().Values.push_back (value);
m_Groups.back ().Weights.push_back (weight);
}
}
}

Expand All @@ -83,10 +100,13 @@ public:
RandomizeObject (Type &out)
{
Initialise ();
for (const auto &i : m_Values)
for (const auto &i : m_Groups)
{
if (DoesElementExist (i, out))
out = GetRandomElement (i);
if (DoesElementExist (i.Values, out))
{
out = i.Values[RandomWeighed (i.Weights)];
break;
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/mission/missions_Flow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "common/logger.hh"
#include "mission/missions_Funcs.hh"
#include "mission/missions_Globals.hh"
#include "mission/missions_PlayerSwitch.hh"
#include "mission/missions_YscUtils.hh"
#include "mission/missions_Cmds.hh"
#include "missions.hh"
Expand Down Expand Up @@ -110,6 +111,21 @@ MissionRandomizer_Flow::InitStatWatcherForRandomizedMission ()
"Finished initialising Stat Watcher for mission: %d", rId);
}

/*******************************************************/
void
MissionRandomizer_Flow::FixMissionRepeatStructForRandomizedMission ()
{
ePlayerIndex player = ePlayerIndex (MR::sm_PlayerSwitcher.GetDestPlayer ());

Rainbomizer::Logger::LogMessage (
"Updating Mission Repeat Info players - %x to %x, %x to %x",
MR::sm_Globals.g_MissionRepeatInfo->Player, player,
MR::sm_Globals.g_MissionRepeatInfo2->Player, player);

MR::sm_Globals.g_MissionRepeatInfo->Player = player;
MR::sm_Globals.g_MissionRepeatInfo2->Player = player;
}

/*******************************************************/
bool
MissionRandomizer_Flow::PreMissionStart ()
Expand All @@ -132,6 +148,7 @@ MissionRandomizer_Flow::PreMissionStart ()
bMissionStartupFinished = false;
nMissionPtrsSema = 2;

FixMissionRepeatStructForRandomizedMission ();
InitStatWatcherForRandomizedMission ();
SetHeistFlowControlVariables ();

Expand Down Expand Up @@ -249,6 +266,7 @@ MissionRandomizer_Flow::OnMissionStart ()
}

// Need to do this again for mission fails.
FixMissionRepeatStructForRandomizedMission ();
InitStatWatcherForRandomizedMission ();
MR::sm_Cmds.OnMissionStart (OriginalMission->nHash,
RandomizedMission->nHash);
Expand Down
1 change: 1 addition & 0 deletions src/mission/missions_Flow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class MissionRandomizer_Flow

void HandleCurrentMissionChanges ();
void InitStatWatcherForRandomizedMission ();
void FixMissionRepeatStructForRandomizedMission ();
bool HandleCutscenesForRandomizedMission ();

bool HandleHeistCrewRandomization (scrThreadContext *ctx);
Expand Down
17 changes: 17 additions & 0 deletions src/mission/missions_Globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,15 @@ struct MissionDefinition
};
static_assert (sizeof (MissionDefinition) == 0x22 * 8);

/*******************************************************/
struct MissionRepeatInfo
{
alignas (8) ePlayerIndex Player;

// ...
// There are more fields, but these aren't required (yet).
};

/*******************************************************/
struct MissionFlowCommand
{
Expand Down Expand Up @@ -486,6 +495,12 @@ public:
"5e ? ? ? 46 ? ? 35 ? 28 6d 05 ad 1f", 1, "flow_controller"_joaat,
YscUtils::GLOBAL_U24_IOFFSET_S16};

YscUtils::ScriptGlobal<MissionRepeatInfo> g_MissionRepeatInfo{
"25 3e 57 ? ? 70 60 ? ? ? 70 60 ? ? ?", 7, "flow_controller"_joaat};

YscUtils::ScriptGlobal<MissionRepeatInfo> g_MissionRepeatInfo2{
"25 3e 57 ? ? 70 60 ? ? ? 70 60 ? ? ?", 12, "flow_controller"_joaat};

YscUtils::ScriptGlobal<uint32_t> g_BoardInitStateBitset{
"2d 01 03 00 ? 5f ? ? ? 38 ? 2c ? ? ? 2e 01 01", 6,
"jewelry_heist"_joaat};
Expand Down Expand Up @@ -566,6 +581,8 @@ public:
g_ForceWalking.Init (program);
g_MissionFlowCommands.Init (program);
g_Missions.Init (program);
g_MissionRepeatInfo.Init (program);
g_MissionRepeatInfo2.Init (program);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mission/missions_PlayerSwitch.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MissionRandomizer_PlayerSwitcher
public:
struct Context
{
ePlayerIndex destPlayer;
ePlayerIndex destPlayer = ePlayerIndex::PLAYER_MICHAEL;

bool noSetPos = false;
rage::Vec3V destPos;
Expand Down

0 comments on commit 0e6da15

Please sign in to comment.