27 #include "CoreUObject.h"
29 #include "UObject/Class.h"
31 #include "Fingerprint.h"
33 #include "Mechanical.h"
34 #include "SubjectiveActorComponent.h"
36 #include "Mechanism.generated.h"
38 DECLARE_STATS_GROUP(TEXT(
"Mechanism"), STATGROUP_Mechanism, STATCAT_Advanced);
40 DECLARE_CYCLE_STAT_EXTERN(TEXT(
"Boot"), STAT_MechanismBoot,
41 STATGROUP_Mechanism, );
42 DECLARE_CYCLE_STAT_EXTERN(TEXT(
"Buffer"), STAT_MechanismBuffer,
43 STATGROUP_Mechanism, );
44 DECLARE_CYCLE_STAT_EXTERN(TEXT(
"Find Matching Belts"), STAT_MechanismFindMatchingBelts,
45 STATGROUP_Mechanism, );
46 DECLARE_CYCLE_STAT_EXTERN(TEXT(
"Evaluate"), STAT_MechanismEvaluate,
47 STATGROUP_Mechanism, );
48 DECLARE_CYCLE_STAT_EXTERN(TEXT(
"Evaluate ~ Input"), STAT_MechanismEvaluateInput,
49 STATGROUP_Mechanism, );
50 DECLARE_CYCLE_STAT_EXTERN(TEXT(
"Evaluate ~ Steady"),
51 STAT_MechanismEvaluateSteady, STATGROUP_Mechanism, );
52 DECLARE_CYCLE_STAT_EXTERN(TEXT(
"Evaluate ~ Presentation"),
53 STAT_MechanismEvaluatePresentation,
54 STATGROUP_Mechanism, );
55 DECLARE_CYCLE_STAT_EXTERN(TEXT(
"Fetch"), STAT_MechanismFetchDetails,
56 STATGROUP_Mechanism, );
69 static TArray<TWeakObjectPtr<UBelt>> Belts;
74 static TArray<TWeakInterfacePtr<IMechanical>> Mechanicals;
79 static TMap<FFilter, TArray<TWeakObjectPtr<UBelt>>> BeltsByFilters;
84 static TSet<TWeakInterfacePtr<ISubjective>> All;
89 static TArray<TWeakInterfacePtr<ISubjective>> AllNotBooted;
91 static void DoRegister(TScriptInterface<ISubjective> Subjective);
93 static void DoUnregister(TScriptInterface<ISubjective> Subjective);
95 static void DoHandleChange(TScriptInterface<ISubjective> Subjective);
97 static void DoHandleChange(TScriptInterface<ISubjective> Subjective,
98 const TArray<UDetail*>& Details);
100 static class UBelt* DoBufferBelt(
class UBelt* BufferingBelt,
104 static class UBelt* DoBufferBelts(
class UBelt* BufferingBelt,
105 const TArray<class UBelt*>& TargetBelts,
108 static FORCEINLINE
void DoClearMatchingBeltsCache()
110 BeltsByFilters.Reset();
116 static FORCEINLINE
void ClearMatchingBeltsCache()
118 DoClearMatchingBeltsCache();
122 DoObtainMostSpecificBelt(UObject* BeltOwner,
123 TScriptInterface<ISubjective> Subjective);
125 static void DoFindMatchingBelts(
const struct FFilter& InFilter,
126 TArray<class UBelt*>& OutBelts);
128 static FORCEINLINE
void
129 DoGetBeltsMatchingFingerprint(
const struct FFingerprint& Fingerprint,
130 TArray<class UBelt*>& OutBelts)
132 return DoFindMatchingBelts(
FFilter(Fingerprint), OutBelts);
135 static void DoFetchDetailsInAllBelts();
140 static void FetchDetailsInAllBelts();
142 static TWeakObjectPtr<class UMechanism> Instance;
151 FORCEINLINE
static const TSet<TWeakInterfacePtr<ISubjective>>&
GetAll()
167 UFUNCTION(BlueprintCallable, BlueprintPure, Category =
"Apparatus")
169 GetBeltsMatchingFingerprint(const struct
FFingerprint& Fingerprint,
170 TArray<class
UBelt*>& OutBelts);
175 UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Apparatus")
177 FindMatchingBelts(const struct
FFilter& Filter,
178 TArray<class
UBelt*>& OutBelts);
183 UFUNCTION(BlueprintCallable, Category = "Apparatus")
184 static
void BootAll();
189 UFUNCTION(BlueprintCallable, Category = "Apparatus|Mechanics",
190 Meta = (HidePin = "Mechanical", DefaultToSelf = "Mechanical"))
192 BufferBelt(UObject* Mechanical,
199 UFUNCTION(BlueprintCallable, Category = "Apparatus|Mechanics",
200 Meta = (HidePin = "Mechanical", DefaultToSelf = "Mechanical"))
202 BufferBelts(UObject* Mechanical,
203 const TArray<class
UBelt*>& TargetBelts,
209 static
void HandleChange(TScriptInterface<
ISubjective> Subjective);
214 static
void HandleChange(TScriptInterface<
ISubjective> Subjective,
215 const TArray<
UDetail*>& Details);
222 static
void RegisterSubjective(TScriptInterface<
ISubjective> Subjective);
227 static
void UnregisterSubjective(TScriptInterface<
ISubjective> Subjective);
233 ObtainMostSpecificBelt(UObject* BeltOwner,
244 if (Instance.IsValid())
246 return Instance.Get();
248 Instance = NewObject<UMechanism>(GetTransientPackage());
249 check(Instance.IsValid());
250 Instance->AddToRoot();
251 return Instance.Get();
259 static void RegisterMechanical(TScriptInterface<IMechanical> Mechanical);
269 Mechanicals.Remove(Mechanical.GetObject());
273 FORCEINLINE
class UBelt* UMechanism::DoBufferBelt(
274 UBelt* BufferingBelt,
284 SCOPE_CYCLE_COUNTER(STAT_MechanismBuffer);
287 return BufferingBelt;
290 FORCEINLINE
class UBelt* UMechanism::DoBufferBelts(
291 UBelt* BufferingBelt,
292 const TArray<class UBelt*>& TargetBelts,
300 SCOPE_CYCLE_COUNTER(STAT_MechanismBuffer);
303 return BufferingBelt;
306 inline void UMechanism::DoFetchDetailsInAllBelts()
308 SCOPE_CYCLE_COUNTER(STAT_MechanismFetchDetails);
309 for (
auto Belt : Belts)
317 FORCEINLINE
class UBelt*
322 IMechanical* M = CastChecked<IMechanical>(Mechanical);
326 FORCEINLINE
class UBelt*
328 const TArray<class UBelt*>& TargetBelts,
331 IMechanical* M = CastChecked<IMechanical>(Mechanical);
335 FORCEINLINE
void UMechanism::DoHandleChange(TScriptInterface<ISubjective> Subjective)
338 DoObtainMostSpecificBelt(GetTransientPackage(), Subjective)->
Refresh(Subjective);
341 FORCEINLINE
void UMechanism::DoHandleChange(TScriptInterface<ISubjective> Subjective,
342 const TArray<UDetail*>& Details)
345 DoObtainMostSpecificBelt(GetTransientPackage(), Subjective)->
Refresh(Subjective, Details);
350 DoHandleChange(Subjective);
355 TArray<class UBelt*>& OutBelts)
357 DoFindMatchingBelts(Filter, OutBelts);
362 TArray<class UBelt*>& OutBelts)
364 DoGetBeltsMatchingFingerprint(Fingerprint, OutBelts);
369 const TArray<UDetail*>& Details)
371 DoHandleChange(Subjective, Details);
376 DoRegister(Subjective);
381 DoUnregister(Subjective);
387 Mechanicals.Add(Mechanical.GetObject());
389 for (int32 i = 0; i < AllNotBooted.Num(); ++i)
391 if (!AllNotBooted[i].IsValid())
393 AllNotBooted.RemoveAt(i--);
396 if (AllNotBooted[i]->GetSlot() !=
nullptr)
416 const bool bIncludeDisabled =
true;
431 Detail = NewObject<UDetail>(this->_getUObject(), DetailType);
437 checkSlow(FindDetailByType(DetailType));
447 const bool bReuseInactive)
457 if (UNLIKELY(DetailType == UDetail::StaticClass()))
463 if (!Detail->IsEnabled())
466 Detail->SetEnabled(
true);
478 if (Detail->IsEnabled())
480 if (Detail->IsA(DetailType))
483 Detail->SetEnabled(
true);
492 UDetail* Detail = NewObject<UDetail>(this->_getUObject(), DetailType);
498 checkSlow(FindDetailByType(DetailType));
A common interface for all mechanisms.
Definition: Mechanical.h:43
virtual UBelt * GetBufferingBelt() const
A special callback to process a newly created subjects boot process.
Definition: Mechanical.h:83
An interface for all sorts of subjects.
Definition: Subjective.h:42
virtual TArray< UDetail * > & GetDetailsRef()
Direct access for the internal details array.
Definition: Subjective.h:75
class UDetail * EnableDetail(TSubclassOf< UDetail > DetailType)
Enable a detail of a certain type.
Definition: Mechanism.h:404
virtual FFingerprint & GetFingerprint()
Get the internal fingerprint of the subjective.
Definition: Subjective.h:93
UDetail * FindDetail(TSubclassOf< UDetail > DetailType, const bool bIncludeDisabled=false) const
Find the detail by a type.
Definition: Subjective.h:286
class UDetail * AddDetail(TSubclassOf< UDetail > DetailType, const bool bReuseInactive=false)
Add a new active detail or reuse an inactive one.
Definition: Mechanism.h:446
The conveyor belt consisting of subjects.
Definition: Belt.h:60
void BufferDetailsFromBelts(const TArray< class UBelt * > &Belts, const FFilter &TargetFilter)
Fetch the details from other belts, using the specified filter.
Definition: Belt.cpp:314
void FetchDetails()
Fetch all of the current active details in this belt.
Definition: Belt.cpp:267
void BufferDetailsFromBelt(UBelt *Belt, const FFilter &TargetFilter)
Fetch the details from another belt, using the specified filter.
Definition: Belt.cpp:288
void Refresh(TScriptInterface< ISubjective > Subjective)
Refresh a subjective within the belt.
Definition: Belt.cpp:164
The base subject detail (component) class.
Definition: Detail.h:35
bool IsEnabled() const
Check if the detail currently active.
Definition: Detail.h:56
virtual void Activated()
The event is fired, when the detail has become active for the subject.
Definition: Detail.h:105
void SetEnabled(const bool bState=true)
Set a detail to be active, or not.
Definition: Detail.cpp:28
The main Mechanism function library.
Definition: Mechanism.h:63
static const TSet< TWeakInterfacePtr< ISubjective > > & GetAll()
All of the subjectives currently available and registered.
Definition: Mechanism.h:151
static void RegisterMechanical(TScriptInterface< IMechanical > Mechanical)
Register a mechanical within the mechanism.
Definition: Mechanism.h:384
static class UBelt * BufferBelts(UObject *Mechanical, const TArray< class UBelt * > &TargetBelts, const FFilter &Filter)
Buffer multiple belts with a filter.
Definition: Mechanism.h:327
static const TArray< TWeakInterfacePtr< ISubjective > > & GetAllNotBooted()
All of the subjectives available that are not yet booted.
Definition: Mechanism.h:159
static void GetBeltsMatchingFingerprint(const struct FFingerprint &Fingerprint, TArray< class UBelt * > &OutBelts)
Get all of the belts matching a fingerprint.
Definition: Mechanism.h:361
static void BootAll()
Boot all of the subjectives currently available.
Definition: Mechanism.cpp:44
static void UnregisterMechanical(TScriptInterface< IMechanical > Mechanical)
Unregister a mechanical from the mechanism.
Definition: Mechanism.h:266
static void UnregisterSubjective(TScriptInterface< ISubjective > Subjective)
Remove a subjective from the mechanism completely.
Definition: Mechanism.h:379
static class UBelt * BufferBelt(UObject *Mechanical, class UBelt *Belt, const FFilter &Filter)
Buffer a single belt with a filter.
Definition: Mechanism.h:318
static void FindMatchingBelts(const struct FFilter &Filter, TArray< class UBelt * > &OutBelts)
Get all of the belts matching a filter.
Definition: Mechanism.h:354
static void HandleChange(TScriptInterface< ISubjective > Subjective)
Handle the subjective change.
Definition: Mechanism.h:348
static void RegisterSubjective(TScriptInterface< ISubjective > Subjective)
Register a subjective within the mechanism.
Definition: Mechanism.h:374
The details filter specification.
Definition: Filter.h:44
EBootFilter GetBootFilter() const
Get the active boot filter.
Definition: Filter.h:83
The details fingerprint.
Definition: Fingerprint.h:117
bool Add(std::initializer_list< TSubclassOf< class UDetail >> DetailTypes)
Add detail types to a fingerprint.