Skip to content

Commit

Permalink
ChaosMod: Improvements to "Spawn Companion" effects (#3703)
Browse files Browse the repository at this point in the history
- Extracted common code to set relationships
- Made spawned peds like / hate the same entities player does

---------

Co-authored-by: Reguas <[email protected]>
  • Loading branch information
Regynate and Regynate authored Jan 27, 2025
1 parent 8dddd3a commit cb6a8d1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 24 deletions.
2 changes: 2 additions & 0 deletions ChaosMod/Components/LuaScripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Util/EntityIterator.h"
#include "Util/File.h"
#include "Util/HelpText.h"
#include "Util/Peds.h"
#include "Util/Player.h"
#include "Util/PoolSpawner.h"
#include "Util/Script.h"
Expand Down Expand Up @@ -317,6 +318,7 @@ static const std::vector<ExposableFunc> ms_Exposables {
E("LoadModel", LoadModel),
E("GetAllPeds", GetAllPedsArray),
E("CreatePoolPed", CreatePoolPed),
E("SetCompanionRelationship", SetCompanionRelationship),
E("TeleportPlayer", [](float x, float y, float z, bool noOffset) { TeleportPlayer(x, y, z, noOffset); }),
E("GetAllVehicles", GetAllVehsArray),
E("CreatePoolVehicle", CreatePoolVehicle),
Expand Down
10 changes: 4 additions & 6 deletions ChaosMod/Effects/db/Peds/PedsSpawnCompanionBrad.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#include <stdafx.h>

#include "Util/Peds.h"

#include "Effects/Register/RegisterEffect.h"

static void OnStart()
{
static const Hash model = "ig_brad"_hash;

Hash relationshipGroup;
ADD_RELATIONSHIP_GROUP("_COMPANION_BRAD", &relationshipGroup);
SET_RELATIONSHIP_BETWEEN_GROUPS(0, relationshipGroup, "PLAYER"_hash);
SET_RELATIONSHIP_BETWEEN_GROUPS(0, "PLAYER"_hash, relationshipGroup);

Ped playerPed = PLAYER_PED_ID();
Vector3 playerPos = GET_ENTITY_COORDS(playerPed, false);

Expand All @@ -22,7 +19,8 @@ static void OnStart()

SET_PED_SUFFERS_CRITICAL_HITS(ped, false);

SET_PED_RELATIONSHIP_GROUP_HASH(ped, relationshipGroup);
SetCompanionRelationship(ped, "_COMPANION_BRAD");

SET_PED_HEARING_RANGE(ped, 9999.f);

SET_PED_AS_GROUP_MEMBER(ped, GET_PLAYER_GROUP(PLAYER_ID()));
Expand Down
9 changes: 3 additions & 6 deletions ChaosMod/Effects/db/Peds/PedsSpawnCompanionChimp.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#include <stdafx.h>

#include "Util/Peds.h"

#include "Effects/Register/RegisterEffect.h"

static void OnStart()
{
static const Hash modelHash = "a_c_chimp"_hash;

Hash relationshipGroup;
ADD_RELATIONSHIP_GROUP("_COMPANION_CHIMP", &relationshipGroup);
SET_RELATIONSHIP_BETWEEN_GROUPS(0, relationshipGroup, "PLAYER"_hash);
SET_RELATIONSHIP_BETWEEN_GROUPS(0, "PLAYER"_hash, relationshipGroup);

Ped playerPed = PLAYER_PED_ID();
Vector3 playerPos = GET_ENTITY_COORDS(playerPed, false);

Expand All @@ -21,7 +18,7 @@ static void OnStart()

SET_PED_SUFFERS_CRITICAL_HITS(ped, false);

SET_PED_RELATIONSHIP_GROUP_HASH(ped, relationshipGroup);
SetCompanionRelationship(ped, "_COMPANION_CHIMP");
SET_PED_HEARING_RANGE(ped, 9999.f);

SET_PED_AS_GROUP_MEMBER(ped, GET_PLAYER_GROUP(PLAYER_ID()));
Expand Down
9 changes: 3 additions & 6 deletions ChaosMod/Effects/db/Peds/PedsSpawnCompanionChop.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
#include <stdafx.h>

#include "Util/Peds.h"

#include "Effects/Register/RegisterEffect.h"

static void OnStart()
{
static const Hash modelHash = "a_c_chop"_hash;

Hash relationshipGroup;
ADD_RELATIONSHIP_GROUP("_COMPANION_CHOP", &relationshipGroup);
SET_RELATIONSHIP_BETWEEN_GROUPS(0, relationshipGroup, "PLAYER"_hash);
SET_RELATIONSHIP_BETWEEN_GROUPS(0, "PLAYER"_hash, relationshipGroup);

Ped playerPed = PLAYER_PED_ID();
Vector3 playerPos = GET_ENTITY_COORDS(playerPed, false);

Ped ped = CreatePoolPed(28, modelHash, playerPos.x, playerPos.y, playerPos.z, GET_ENTITY_HEADING(playerPed));
CurrentEffect::SetEffectSoundPlayOptions({ .PlayType = EffectSoundPlayType::FollowEntity, .Entity = ped });
SET_PED_COMBAT_ATTRIBUTES(ped, 0, false);
SET_PED_RELATIONSHIP_GROUP_HASH(ped, relationshipGroup);
SetCompanionRelationship(ped, "_COMPANION_CHOP");
SET_PED_HEARING_RANGE(ped, 9999.f);

SET_PED_AS_GROUP_MEMBER(ped, GET_PLAYER_GROUP(PLAYER_ID()));
Expand Down
9 changes: 3 additions & 6 deletions ChaosMod/Effects/db/Peds/PedsSpawnCompanionRandom.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include <stdafx.h>

#include "Util/Peds.h"

#include "Effects/Register/RegisterEffect.h"

static void OnStart()
{
Hash relationshipGroup;
ADD_RELATIONSHIP_GROUP("_COMPANION_RANDOM", &relationshipGroup);
SET_RELATIONSHIP_BETWEEN_GROUPS(0, relationshipGroup, "PLAYER"_hash);
SET_RELATIONSHIP_BETWEEN_GROUPS(0, "PLAYER"_hash, relationshipGroup);

Ped playerPed = PLAYER_PED_ID();
Vector3 playerPos = GET_ENTITY_COORDS(playerPed, false);

Expand All @@ -19,7 +16,7 @@ static void OnStart()

SET_PED_SUFFERS_CRITICAL_HITS(ped, false);

SET_PED_RELATIONSHIP_GROUP_HASH(ped, relationshipGroup);
SetCompanionRelationship(ped, "_COMPANION_RANDOM");
SET_PED_HEARING_RANGE(ped, 9999.f);

SET_PED_AS_GROUP_MEMBER(ped, GET_PLAYER_GROUP(PLAYER_ID()));
Expand Down
21 changes: 21 additions & 0 deletions ChaosMod/Util/Peds.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,24 @@ inline Ped CreateHostilePed(Hash modelHash, Hash weaponHash, Vector3 *location =
SET_PED_FIRING_PATTERN(ped, 0xC6EE6B4C);
return ped;
}

inline void SetCompanionRelationship(Ped ped, const std::string &name)
{
static const Hash playerGroup = "PLAYER"_hash;
Hash relationshipGroup;
ADD_RELATIONSHIP_GROUP(name.c_str(), &relationshipGroup);
SET_RELATIONSHIP_BETWEEN_GROUPS(0, relationshipGroup, playerGroup);
SET_RELATIONSHIP_BETWEEN_GROUPS(0, playerGroup, relationshipGroup);
for (Ped ped : GetAllPeds())
{
Hash group = GET_PED_RELATIONSHIP_GROUP_HASH(ped);
if (_DOES_RELATIONSHIP_GROUP_EXIST(group))
{
SET_RELATIONSHIP_BETWEEN_GROUPS(GET_RELATIONSHIP_BETWEEN_GROUPS(playerGroup, group), relationshipGroup,
group);
SET_RELATIONSHIP_BETWEEN_GROUPS(GET_RELATIONSHIP_BETWEEN_GROUPS(group, playerGroup), group,
relationshipGroup);
}
}
SET_PED_RELATIONSHIP_GROUP_HASH(ped, relationshipGroup);
}

0 comments on commit cb6a8d1

Please sign in to comment.