Skip to content

Commit

Permalink
feat: reverse engineered fallout 4 magic classes
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed Nov 23, 2021
1 parent bf77eae commit b05b415
Show file tree
Hide file tree
Showing 27 changed files with 257 additions and 25 deletions.
3 changes: 1 addition & 2 deletions Code/client/Games/Fallout4/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <Forms/TESForm.h>
#include <Misc/ActorState.h>
#include <Misc/MagicTarget.h>
#include <Magic/MagicTarget.h>
#include <Structs/ActorValues.h>
#include <Structs/Factions.h>
#include <Structs/Inventory.h>
Expand Down Expand Up @@ -68,7 +68,6 @@ struct Actor : TESObjectREFR
void Respawn() noexcept;

MagicTarget magicTarget;
uint8_t unk118[0x128 - 0x118];
ActorState actorState;
BSTEventSink<BSMovementDataChangedEvent> movementDataChangedSink;
BSTEventSink<BSTransformDeltaEvent> transformDeltaSink;
Expand Down
8 changes: 8 additions & 0 deletions Code/client/Games/Fallout4/Magic/ActorMagicCaster.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "MagicCaster.h"

struct ActorMagicCaster : MagicCaster
{
uint8_t pad48[0xB8];
};
19 changes: 19 additions & 0 deletions Code/client/Games/Fallout4/Magic/EffectItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

struct EffectSetting;

struct EffectItemData
{
float fMagnitude;
int32_t iArea;
int32_t iDuration;
};

struct EffectItem
{
EffectItemData data;
EffectSetting* pEffectSetting;
float fRawCost;
void* Conditions;
};

14 changes: 14 additions & 0 deletions Code/client/Games/Fallout4/Magic/EffectSetting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <Forms/TESForm.h>
#include <Games/Magic/MagicSystem.h>

struct EffectSetting : TESForm
{
uint8_t pad20[0xD0 - 0x20];
EffectArchetypes::ArchetypeID eArchetype;
uint8_t padD4[0x1B0 - 0xD4];
};

static_assert(sizeof(EffectSetting) == 0x1B0);

31 changes: 31 additions & 0 deletions Code/client/Games/Fallout4/Magic/MagicCaster.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

struct MagicCaster
{
virtual ~MagicCaster();

enum State : int32_t
{
DERIVED_ATTRIBUTE = 0x0,
ATTRIBUTE = 0x1,
SKILL = 0x2,
AI_ATTRIBUTE = 0x3,
RESISTANCE = 0x4,
CONDITION = 0x5,
CHARGE = 0x6,
INT_VALUE = 0x7,
VARIABLE = 0x8,
RESOURCE = 0x9,
TYPE_COUNT = 0xA,
};

GameArray<uint32_t> hSounds;
int32_t hDesiredTarget;
MagicItem* pCurrentSpell;
MagicCaster::State eState;
float fCastingTimer;
float fCurrentSpellCost;
float fMagnitudeOverride;
float fNextTargetUpdate;
float fProjectileTimer;
};
17 changes: 17 additions & 0 deletions Code/client/Games/Fallout4/Magic/MagicItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <Forms/TESBoundObject.h>
#include <Components/TESFullName.h>
#include <Components/BGSKeywordForm.h>

struct EffectItem;
struct EffectSetting;

struct MagicItem : TESBoundObject, TESFullName, BGSKeywordForm
{
GameArray<EffectItem*> listOfEffects;
int32_t iHostileCount;
EffectSetting* pAVEffectSetting;
uint32_t uiPreloadCount;
void* spPreloadedItem;
};
97 changes: 97 additions & 0 deletions Code/client/Games/Fallout4/Magic/MagicTarget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "MagicTarget.h"

#include <Actor.h>
#include <Games/ActorExtension.h>
#include <World.h>
#include <Games/ActorExtension.h>
#include "EffectItem.h"
#include "EffectSetting.h"
#include "MagicItem.h"

#include <Events/AddTargetEvent.h>

TP_THIS_FUNCTION(TAddTarget, bool, MagicTarget, MagicTarget::AddTargetData& arData);
TP_THIS_FUNCTION(TCheckAddEffectTargetData, bool, MagicTarget::AddTargetData, void* arArgs, float afResistance);

static TAddTarget* RealAddTarget = nullptr;
static TCheckAddEffectTargetData* RealCheckAddEffectTargetData = nullptr;

static thread_local bool s_autoSucceedEffectCheck = false;

bool MagicTarget::AddTarget(AddTargetData& arData) noexcept
{
s_autoSucceedEffectCheck = true;
bool result = ThisCall(RealAddTarget, this, arData);
s_autoSucceedEffectCheck = false;
return result;
}

bool TP_MAKE_THISCALL(HookAddTarget, MagicTarget, MagicTarget::AddTargetData& arData)
{
// TODO: this can be fixed by properly implementing multiple inheritance
Actor* pTargetActor = (Actor*)((char*)apThis - 0x98);
ActorExtension* pTargetActorEx = pTargetActor->GetExtension();

if (!pTargetActorEx)
return ThisCall(RealAddTarget, apThis, arData);

if (pTargetActorEx->IsLocalPlayer())
{
bool result = ThisCall(RealAddTarget, apThis, arData);
if (result && arData.pEffectItem->pEffectSetting->eArchetype != EffectArchetypes::ArchetypeID::SUMMON_CREATURE)
World::Get().GetRunner().Trigger(AddTargetEvent(pTargetActor->formID, arData.pSpell->formID));
return result;
}
else if (pTargetActorEx->IsRemotePlayer())
{
return false;
}

if (arData.pCaster)
{
ActorExtension* pCasterExtension = arData.pCaster->GetExtension();
if (pCasterExtension->IsLocalPlayer())
{
bool result = ThisCall(RealAddTarget, apThis, arData);
if (result && arData.pEffectItem->pEffectSetting->eArchetype != EffectArchetypes::ArchetypeID::SUMMON_CREATURE)
World::Get().GetRunner().Trigger(AddTargetEvent(pTargetActor->formID, arData.pSpell->formID));
return result;
}
else if (pCasterExtension->IsRemotePlayer())
{
return false;
}
}

if (pTargetActorEx->IsLocal())
{
bool result = ThisCall(RealAddTarget, apThis, arData);
if (result && arData.pEffectItem->pEffectSetting->eArchetype != EffectArchetypes::ArchetypeID::SUMMON_CREATURE)
World::Get().GetRunner().Trigger(AddTargetEvent(pTargetActor->formID, arData.pSpell->formID));
return result;
}
else
{
return false;
}
}

bool TP_MAKE_THISCALL(HookCheckAddEffectTargetData, MagicTarget::AddTargetData, void* arArgs, float afResistance)
{
if (s_autoSucceedEffectCheck)
return true;

return ThisCall(RealCheckAddEffectTargetData, apThis, arArgs, afResistance);
}

static TiltedPhoques::Initializer s_magicTargetHooks([]() {
POINTER_FALLOUT4(TAddTarget, addTarget, 0x140C6C5E0 - 0x140000000);
POINTER_FALLOUT4(TCheckAddEffectTargetData, checkAddEffectTargetData, 0x140C6D4F0 - 0x140000000);

RealAddTarget = addTarget.Get();
RealCheckAddEffectTargetData = checkAddEffectTargetData.Get();

TP_HOOK(&RealAddTarget, HookAddTarget);
TP_HOOK(&RealCheckAddEffectTargetData, HookCheckAddEffectTargetData);
});

53 changes: 53 additions & 0 deletions Code/client/Games/Fallout4/Magic/MagicTarget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <Effects/ActiveEffect.h>
#include <Games/Magic/MagicSystem.h>

struct Actor;
struct EffectItem;
struct MagicItem;
struct TESBoundObject;

struct MagicTarget
{
struct IPostCreationModification;
struct ResultsCollector;

struct AddTargetData
{
Actor* pCaster;
MagicItem* pSpell;
EffectItem* pEffectItem;
TESBoundObject* pSource;
MagicTarget::IPostCreationModification* pCallback;
MagicTarget::ResultsCollector* pResultsCollector;
NiPoint3 ExplosionLocation;
float fMagnitude;
MagicSystem::CastingSource eCastingSource;
bool bAreaTarget;
bool bDualCast;
};

struct SpellDispelData
{
const MagicItem* pSpell;
int32_t hCaster;
GamePtr<ActiveEffect> spActiveEffect;
SpellDispelData* pNext;
};

enum Flag : int32_t
{
MTF_UPDATING = 0x1,
MTF_INVISIBLE = 0x2,
};

virtual ~MagicTarget();

bool AddTarget(AddTargetData& arData) noexcept;
// this function actually adds the effect
bool CheckAddEffect(AddTargetData& arData) noexcept;

SpellDispelData* pPostUpdateDispelList;
Flag ucFlags;
};
6 changes: 0 additions & 6 deletions Code/client/Games/Fallout4/Misc/MagicTarget.h

This file was deleted.

2 changes: 1 addition & 1 deletion Code/client/Games/Fallout4/Projectiles/Projectile.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <Games/Misc/MagicSystem.h>
#include <Games/Magic/MagicSystem.h>

struct BGSProjectile;
struct TESObjectREFR;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion Code/client/Games/References.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <Games/TES.h>
#include <Games/Overrides.h>
#include <Games/Misc/Lock.h>
#include <Games/Skyrim/Misc/MagicCaster.h>
#include <Magic/MagicCaster.h>

#include <Events/LockChangeEvent.h>

Expand Down
4 changes: 2 additions & 2 deletions Code/client/Games/Skyrim/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <Games/Events.h>
#include <TESObjectREFR.h>

#include <Misc/MagicTarget.h>
#include <Magic/MagicTarget.h>
#include <Forms/TESActorBase.h>
#include <Misc/ActorState.h>
#include <Misc/IPostAnimationChannelUpdateFunctor.h>
#include <Forms/MagicItem.h>
#include <Games/Skyrim/Misc/ActorMagicCaster.h>
#include <Magic/ActorMagicCaster.h>

#include <Structs/Inventory.h>
#include <Structs/Factions.h>
Expand Down
2 changes: 1 addition & 1 deletion Code/client/Games/Skyrim/Effects/ActiveEffect.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <Games/Misc/MagicSystem.h>
#include <Games/Magic/MagicSystem.h>

struct MagicTarget;
struct EffectItem;
Expand Down
2 changes: 1 addition & 1 deletion Code/client/Games/Skyrim/Forms/EffectSetting.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <Components/TESFullName.h>
#include <Components/BGSKeywordForm.h>
#include <Components/BGSMenuDisplayObject.h>
#include <Games/Misc/MagicSystem.h>
#include <Games/Magic/MagicSystem.h>


struct EffectSetting : TESForm
Expand Down
2 changes: 1 addition & 1 deletion Code/client/Games/Skyrim/Forms/MagicItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <Forms/TESBoundObject.h>
#include <Forms/EffectSetting.h>
#include <Misc/EffectItem.h>
#include <Magic/EffectItem.h>
#include <Components/BGSKeywordForm.h>
#include <Components/TESFullName.h>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

struct EffectSetting;

struct EffectItemData
{
float fMagnitude;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <Games/Misc/MagicSystem.h>
#include <Games/Magic/MagicSystem.h>

struct ActiveEffect;
struct MagicItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Games/ActorExtension.h>
#include <World.h>
#include <Games/ActorExtension.h>
#include "EffectItem.h"

#include <Events/AddTargetEvent.h>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <Games/Misc/MagicSystem.h>
#include <Games/Magic/MagicSystem.h>

struct Actor;
struct MagicItem;
Expand Down
2 changes: 1 addition & 1 deletion Code/client/Games/Skyrim/Projectiles/Projectile.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Games/Skyrim/TESObjectREFR.h>
#include <Games/Misc/MagicSystem.h>
#include <Games/Magic/MagicSystem.h>

struct BGSProjectile;
struct TESObjectREFR;
Expand Down
4 changes: 2 additions & 2 deletions Code/client/Games/Skyrim/TESObjectREFR.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <ExtraData/ExtraContainerChanges.h>
#include <Games/Animation/IAnimationGraphManagerHolder.h>
#include <Games/Misc/Lock.h>
#include <Games/Misc/MagicSystem.h>
#include <Games/Skyrim/Misc/MagicCaster.h>
#include <Games/Magic/MagicSystem.h>
#include <Magic/MagicCaster.h>

struct AnimationVariables;
struct TESWorldSpace;
Expand Down
5 changes: 1 addition & 4 deletions Code/client/Services/Generic/MagicService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
#include <Messages/NotifyAddTarget.h>

#include <Actor.h>

#if TP_SKYRIM64
#include <Misc/ActorMagicCaster.h>
#endif
#include <Magic/ActorMagicCaster.h>

MagicService::MagicService(World& aWorld, entt::dispatcher& aDispatcher, TransportService& aTransport) noexcept
: m_world(aWorld), m_dispatcher(aDispatcher), m_transport(aTransport)
Expand Down
Loading

0 comments on commit b05b415

Please sign in to comment.