From cb6a8d164f483faaa8432e208ece80cede4d4a57 Mon Sep 17 00:00:00 2001 From: Regynate <64607261+Regynate@users.noreply.github.com> Date: Mon, 27 Jan 2025 21:55:04 +0300 Subject: [PATCH] ChaosMod: Improvements to "Spawn Companion" effects (#3703) - Extracted common code to set relationships - Made spawned peds like / hate the same entities player does --------- Co-authored-by: Reguas <64607261+Reguas@users.noreply.github.com> --- ChaosMod/Components/LuaScripts.cpp | 2 ++ .../db/Peds/PedsSpawnCompanionBrad.cpp | 10 ++++----- .../db/Peds/PedsSpawnCompanionChimp.cpp | 9 +++----- .../db/Peds/PedsSpawnCompanionChop.cpp | 9 +++----- .../db/Peds/PedsSpawnCompanionRandom.cpp | 9 +++----- ChaosMod/Util/Peds.h | 21 +++++++++++++++++++ 6 files changed, 36 insertions(+), 24 deletions(-) diff --git a/ChaosMod/Components/LuaScripts.cpp b/ChaosMod/Components/LuaScripts.cpp index eb3a7534f..f6d5bc0fc 100644 --- a/ChaosMod/Components/LuaScripts.cpp +++ b/ChaosMod/Components/LuaScripts.cpp @@ -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" @@ -317,6 +318,7 @@ static const std::vector 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), diff --git a/ChaosMod/Effects/db/Peds/PedsSpawnCompanionBrad.cpp b/ChaosMod/Effects/db/Peds/PedsSpawnCompanionBrad.cpp index ca9b6069f..b1d883a77 100644 --- a/ChaosMod/Effects/db/Peds/PedsSpawnCompanionBrad.cpp +++ b/ChaosMod/Effects/db/Peds/PedsSpawnCompanionBrad.cpp @@ -1,16 +1,13 @@ #include +#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); @@ -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())); diff --git a/ChaosMod/Effects/db/Peds/PedsSpawnCompanionChimp.cpp b/ChaosMod/Effects/db/Peds/PedsSpawnCompanionChimp.cpp index b83b44a50..27165aeaa 100644 --- a/ChaosMod/Effects/db/Peds/PedsSpawnCompanionChimp.cpp +++ b/ChaosMod/Effects/db/Peds/PedsSpawnCompanionChimp.cpp @@ -1,16 +1,13 @@ #include +#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); @@ -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())); diff --git a/ChaosMod/Effects/db/Peds/PedsSpawnCompanionChop.cpp b/ChaosMod/Effects/db/Peds/PedsSpawnCompanionChop.cpp index b8b21e772..ee82d261b 100644 --- a/ChaosMod/Effects/db/Peds/PedsSpawnCompanionChop.cpp +++ b/ChaosMod/Effects/db/Peds/PedsSpawnCompanionChop.cpp @@ -1,23 +1,20 @@ #include +#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())); diff --git a/ChaosMod/Effects/db/Peds/PedsSpawnCompanionRandom.cpp b/ChaosMod/Effects/db/Peds/PedsSpawnCompanionRandom.cpp index efde11ab0..5f79f1419 100644 --- a/ChaosMod/Effects/db/Peds/PedsSpawnCompanionRandom.cpp +++ b/ChaosMod/Effects/db/Peds/PedsSpawnCompanionRandom.cpp @@ -1,14 +1,11 @@ #include +#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); @@ -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())); diff --git a/ChaosMod/Util/Peds.h b/ChaosMod/Util/Peds.h index d39e0a2b4..5e9bcb66a 100644 --- a/ChaosMod/Util/Peds.h +++ b/ChaosMod/Util/Peds.h @@ -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); +}