Agrarsense
AssetLibrary.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#include "AssetLibrary.h"
7
17
21
22#include "Kismet/KismetSystemLibrary.h"
23#include "Engine/GameViewportClient.h"
24#include "GameFramework/Actor.h"
25#include "Engine/Engine.h"
26#include "UObject/Class.h"
27#include "Engine/World.h"
28
29// Paths to data assets
30static const FString VEHICLE_ASSET_PATH = "/Game/Agrarsense/Data/Vehicles/VehicleActorAssetMap";
31static const FString FOLIAGE_ASSET_PATH = "/Game/Agrarsense/Data/Foliage/FoliageActorAssetMap";
32static const FString WALKER_ASSET_PATH = "/Game/Agrarsense/Data/Walkers/WalkerActorAssetMap";
33static const FString PROP_ASSET_PATH = "/Game/Agrarsense/Data/Props/PropActorAssetMap";
34
35TWeakObjectPtr<UFoliageActorAssetMapDataAsset> UAssetLibrary::FoliageActorAssetMapDataAsset;
36TArray<FInstancedActorData> UAssetLibrary::AddedFoliageActors;
37TMap<EFoliageTypes, FString> UAssetLibrary::FoliageTypeMap;
38TArray<EFoliageTypes> UAssetLibrary::FoliageKeys;
39
40TWeakObjectPtr <UVehicleActorAssetMapDataAsset> UAssetLibrary::VehicleDataAsset;
41TArray<FVehicleData> UAssetLibrary::SpawnedVehicles;
42
43TWeakObjectPtr<UPropActorAssetMapDataAsset> UAssetLibrary::PropActorAssetMapDataAsset;
44TArray<FInstancedActorData> UAssetLibrary::SpawnedPropActors;
45TMap<EPropTypes, FString> UAssetLibrary::PropTypeMap;
46TArray<EPropTypes> UAssetLibrary::PropKeys;
47
48TWeakObjectPtr<UWalkerActorAssetMapDataAsset> UAssetLibrary::WalkerActorAssetMapDataAsset;
49TArray<TWeakObjectPtr<AWalker>> UAssetLibrary::SpawnedWalkers;
50
51AActor* UAssetLibrary::SpawnFoliage(EFoliageTypes FoliageType, FTransform Transform,
52 FString ActorName, FString ActorID,
53 bool RandomZRotation, bool SnapToGround)
54{
56
57 if (!FoliageActorAssetMapDataAsset.IsValid())
58 {
59 return nullptr;
60 }
61
62 UFoliageActorAssetDataAsset* ActorAssetData = FoliageActorAssetMapDataAsset->Foliages.FindRef(FoliageType);
63 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->ActorAsset.Actor : nullptr;
64
65 AActor* SpawnedActor = TrySpawnActor(ActorClass, Transform, ActorName, ActorID, SnapToGround, RandomZRotation);
66
67 AInstancedActor* InstancedActor = Cast<AInstancedActor>(SpawnedActor);
68 if (InstancedActor)
69 {
70 if (FoliageTypeMap.Contains(FoliageType))
71 {
72 // Set InstactedActor type and model strings, this is used for JSON exporting.
73 FString Model = FoliageTypeMap[FoliageType];
74 InstancedActor->SetTypeAndModel("foliage", Model);
75 }
76
77 // Add to array
79 Data.Actor = InstancedActor;
80 AddedFoliageActors.Add(Data);
81 }
82
83 return SpawnedActor;
84}
85
87 FTransform SpawnTransform, const FString& ActorName,
88 const FString& ActorID, bool SnapAboveGround,
89 const float AboveOffset, const bool DestroyOverlappingActors)
90{
92
93 if (!VehicleDataAsset.IsValid())
94 {
95 return nullptr;
96 }
97
98 UVehicleActorAssetDataAsset* ActorAssetData = VehicleDataAsset->Vehicles.FindRef(VehicleType);
99 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->ActorAsset.Actor : nullptr;
100
101 ESpawnActorCollisionHandlingMethod SpawnCollisionHandling = ESpawnActorCollisionHandlingMethod::Undefined;
102 if (VehicleType == EVehicleTypes::Drone)
103 {
104 // Workaround for drone
105 SpawnCollisionHandling = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
106 }
107
108 FString ID = ActorID;
109 if (ID.IsEmpty())
110 {
112 }
113
114 AActor* SpawnedActor = TrySpawnActor(ActorClass, SpawnTransform, ActorName, ID, false, false, SpawnCollisionHandling);
115
116 AVehicle* Vehicle = Cast<AVehicle>(SpawnedActor);
117 if (SpawnedActor && Vehicle)
118 {
119 if (SnapAboveGround)
120 {
122 SpawnTransform = SpawnedActor->GetTransform();
123 }
124
125 FVehicleData VehicleData;
126 VehicleData.Vehicle = Vehicle;
127 VehicleData.VehicleType = VehicleType;
128 SpawnedVehicles.Add(VehicleData);
129
130 Vehicle->TeleportTo(SpawnTransform.GetLocation(), SpawnTransform.GetRotation().Rotator());
131
132 if (DestroyOverlappingActors)
133 {
135 }
136
137 // Add to array
138 FVehicleData Data;
139 Data.Vehicle = Vehicle;
140 Data.VehicleType = Vehicle->GetVehicleType();
141 SpawnedVehicles.Add(Data);
142 }
143
144 return Vehicle;
145}
146
147AActor* UAssetLibrary::SpawnProp(EPropTypes PropType, FTransform Transform,
148 FString ActorName, FString ActorID,
149 bool RandomZRotation, bool SnapToGround)
150{
152
153 if (!PropActorAssetMapDataAsset.IsValid())
154 {
155 return nullptr;
156 }
157
158 UPropActorAssetDataAsset* ActorAssetData = PropActorAssetMapDataAsset->Props.FindRef(PropType);
159 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->ActorAsset.Actor : nullptr;
160
161 AActor* SpawnedActor = TrySpawnActor(ActorClass, Transform, ActorName, ActorID, SnapToGround, RandomZRotation);
162
163 AInstancedActor* InstancedActor = Cast<AInstancedActor>(SpawnedActor);
164 if (InstancedActor)
165 {
166 FInstancedActorData InstancedActorData;
167 InstancedActorData.Actor = InstancedActor;
168 SpawnedPropActors.Add(InstancedActorData);
169
170 if (PropTypeMap.Contains(PropType))
171 {
172 // Set InstactedActor type and model strings, this is used for JSON exporting.
173 FString Model = PropTypeMap[PropType];
174 InstancedActor->SetTypeAndModel("prop", Model);
175 }
176
177
178 // Add to array
180 Data.Actor = InstancedActor;
181 SpawnedPropActors.Add(Data);
182 }
183
184 return SpawnedActor;
185}
186
187AWalker* UAssetLibrary::SpawnWalker(FWalkerParameters Parameters, const FString& ActorName,
188 const FString& ActorID, bool StartAutomatically)
189{
191
192 if (!WalkerActorAssetMapDataAsset.IsValid())
193 {
194 return nullptr;
195 }
196
197 UWalkerActorAssetDataAsset* ActorAssetData = WalkerActorAssetMapDataAsset->Walkers.FindRef(Parameters.WalkerType);
198 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->ActorAsset.Actor : nullptr;
199
200 FTransform StartTransform;
201 auto& Transforms = Parameters.Points;
202
203 if (!Transforms.IsEmpty())
204 {
205 if (Parameters.WalkerAction == EWalkerAction::FollowPath && Transforms.Num() < 2)
206 {
207 SimulatorLog::Log("Failed to spawn FollowPath Walker due to insufficient path points.");
208 return nullptr;
209 }
210 StartTransform = Transforms[0];
211 }
212
213 FString ID = ActorID;
214 if (ID.IsEmpty())
215 {
217 }
218
219 AActor* SpawnedActor = TrySpawnActorDeferred(ActorClass, StartTransform, ActorName, ID, false, false);
220
221 AWalker* Walker = Cast<AWalker>(SpawnedActor);
222 if (Walker)
223 {
225 Walker->SetWalkerParameters(Parameters);
226 Walker->SetIgnoreInput(!StartAutomatically);
227
228 SpawnedActor->FinishSpawning(StartTransform);
229 }
230
231 return Walker;
232}
233
234AActor* UAssetLibrary::TrySpawnActor(TSubclassOf<AActor> ActorClass, FTransform& Transform,
235 FString ActorName, FString ActorID,
236 bool SnapToGround, bool RandomZRotation, ESpawnActorCollisionHandlingMethod CollisionMethod)
237{
238 if (!IsInGameThread())
239 {
240#if WITH_EDITOR
241 UE_LOG(LogTemp, Warning, TEXT("AssetLibrary.cpp: Attempting to spawn actor from a background thread. This is not allowed."));
242#endif
243 return nullptr;
244 }
245
246 if (!ActorClass)
247 {
248#if WITH_EDITOR
249 UE_LOG(LogTemp, Warning, TEXT("AssetLibrary.cpp: ActorClass is nullptr! Cannot spawn actor."));
250#endif
251 return nullptr;
252 }
253
254 UWorld* World = nullptr;
255 if (GEngine && GEngine->GameViewport)
256 {
257 World = GEngine->GameViewport->GetWorld();
258 }
259
260 if (!World)
261 {
262 return nullptr;
263 }
264
265 // Spawn actor deferred
266 AActor* SpawnedActor = World->SpawnActorDeferred<AActor>(ActorClass, Transform, nullptr, nullptr, CollisionMethod);
267
268 if (SpawnedActor)
269 {
270 if (SnapToGround)
271 {
273 Transform = SpawnedActor->GetTransform();
274 }
275
276 if (RandomZRotation)
277 {
278 float RotationAngle = FMath::FRandRange(0.0f, 360.0f);
279 Transform.SetRotation(FQuat::MakeFromEuler(FVector(0.0f, 0.0f, RotationAngle)));
280 }
281
282 // Set ID and name through IActorInformation if the spawned actor implements it
283 if (SpawnedActor && SpawnedActor->GetClass()->ImplementsInterface(UActorInformation::StaticClass()))
284 {
285 IActorInformation::Execute_SetActorIDAndName(SpawnedActor, ActorName, ActorID);
286 }
287
288 // Finish Actor spawning (BeginPlay runs now)
289 SpawnedActor->FinishSpawning(Transform);
290 }
291
292 return SpawnedActor;
293}
294
295AActor* UAssetLibrary::TrySpawnActorDeferred(TSubclassOf<AActor> ActorClass, FTransform& Transform, FString ActorName, FString ActorID, bool SnapToGround, bool RandomZRotation, ESpawnActorCollisionHandlingMethod CollisionMethod)
296{
297 if (!IsInGameThread())
298 {
299#if WITH_EDITOR
300 UE_LOG(LogTemp, Warning, TEXT("AssetLibrary.cpp: Attempting to spawn actor from a background thread. This is not allowed."));
301#endif
302 return nullptr;
303 }
304
305 if (!ActorClass)
306 {
307#if WITH_EDITOR
308 UE_LOG(LogTemp, Warning, TEXT("AssetLibrary.cpp: ActorClass is nullptr! Cannot spawn actor."));
309#endif
310 return nullptr;
311 }
312
313 UWorld* World = nullptr;
314 if (GEngine && GEngine->GameViewport)
315 {
316 World = GEngine->GameViewport->GetWorld();
317 }
318
319 if (!World)
320 {
321 return nullptr;
322 }
323
324 // Spawn actor deferred
325 AActor* SpawnedActor = World->SpawnActorDeferred<AActor>(ActorClass, Transform, nullptr, nullptr, CollisionMethod);
326
327 if (SpawnedActor)
328 {
329 if (SnapToGround)
330 {
332 Transform = SpawnedActor->GetTransform();
333 }
334
335 if (RandomZRotation)
336 {
337 float RotationAngle = FMath::FRandRange(0.0f, 360.0f);
338 Transform.SetRotation(FQuat::MakeFromEuler(FVector(0.0f, 0.0f, RotationAngle)));
339 }
340
341 // Set ID and name through IActorInformation if the spawned actor implements it
342 if (SpawnedActor && SpawnedActor->GetClass()->ImplementsInterface(UActorInformation::StaticClass()))
343 {
344 IActorInformation::Execute_SetActorIDAndName(SpawnedActor, ActorName, ActorID);
345 }
346 }
347
348 return SpawnedActor;
349}
350
352{
353 UWorld* World = nullptr;
354 if (GEngine && GEngine->GameViewport)
355 {
356 World = GEngine->GameViewport->GetWorld();
357 }
358
359 if (!World || !SpawnedActor)
360 {
361 return;
362 }
363
364 FVector Origin;
365 FVector BoxExtent;
366 SpawnedActor->GetActorBounds(true, Origin, BoxExtent);
367
368 float SphereRadius = BoxExtent.GetMax();
369
370 TArray<AActor*> OverlappingActors;
371 UKismetSystemLibrary::SphereOverlapActors(World, SpawnedActor->GetTransform().GetLocation(), SphereRadius, {}, nullptr, {}, OverlappingActors);
372
373 for (AActor* OverlappingActor : OverlappingActors)
374 {
375 if (OverlappingActor && OverlappingActor != SpawnedActor)
376 {
377 if (OverlappingActor->IsA(AWalker::StaticClass()) ||
378 OverlappingActor->IsA(AInstancedActor::StaticClass()) ||
379 OverlappingActor->IsA(AVehicle::StaticClass()))
380 {
381 OverlappingActor->Destroy();
382 }
383 }
384 }
385}
386
388{
389 SpawnedVehicles.RemoveAll([](const FVehicleData& VehicleData)
390 {
391 return !VehicleData.Vehicle.IsValid();
392 });
393
394 return SpawnedVehicles;
395}
396
398{
400
402}
403
405{
407
408 EFoliageTypes RandomFoliageType = EFoliageTypes::NONE;
409 if (FoliageKeys.Num())
410 {
411 int32 RandomIndex = FMath::RandRange(0, FoliageKeys.Num() - 1);
412 RandomFoliageType = FoliageKeys[RandomIndex];
413 }
414
415 return RandomFoliageType;
416}
417
419{
420 AddedFoliageActors.RemoveAll([](const FInstancedActorData& ActorData)
421 {
422 return !ActorData.Actor.IsValid();
423 });
424
425 TArray<AInstancedActor*> ValidActors;
426 ValidActors.Reserve(AddedFoliageActors.Num());
427
428 for (const FInstancedActorData& ActorData : AddedFoliageActors)
429 {
430 if (ActorData.Actor.IsValid())
431 {
432 ValidActors.Add(ActorData.Actor.Get());
433 }
434 }
435
436 return ValidActors;
437}
438
440{
442
443 return PropActorAssetMapDataAsset.Get();
444}
445
447{
449
450 EPropTypes RandomPropType = EPropTypes::NONE;
451 if (PropKeys.Num())
452 {
453 int32 RandomIndex = FMath::RandRange(0, PropKeys.Num() - 1);
454 RandomPropType = PropKeys[RandomIndex];
455 }
456
457 return RandomPropType;
458}
459
460TArray<AInstancedActor*> UAssetLibrary::GetAllAddedPropActors()
461{
462 SpawnedPropActors.RemoveAll([](const FInstancedActorData& ActorData)
463 {
464 return !ActorData.Actor.IsValid();
465 });
466
467 TArray<AInstancedActor*> ValidActors;
468 ValidActors.Reserve(SpawnedPropActors.Num());
469
470 for (const FInstancedActorData& ActorData : SpawnedPropActors)
471 {
472 if (ActorData.Actor.IsValid())
473 {
474 ValidActors.Add(ActorData.Actor.Get());
475 }
476 }
477
478 return ValidActors;
479}
480
482{
484
485 return WalkerActorAssetMapDataAsset.Get();
486}
487
489{
490 SpawnedWalkers.RemoveAll([](const TWeakObjectPtr<AWalker>& ActorData)
491 {
492 return !ActorData.IsValid();
493 });
494
495 TArray<AWalker*> ValidWalkers;
496 ValidWalkers.Reserve(SpawnedWalkers.Num());
497
498 for (const TWeakObjectPtr<AWalker>& ActorData : SpawnedWalkers)
499 {
500 AWalker* WalkerPtr = ActorData.Get();
501 if (WalkerPtr)
502 {
503 ValidWalkers.Add(WalkerPtr);
504 }
505 }
506
507 return ValidWalkers;
508}
509
511{
512 TArray<AWalker*> WalkersActors = GetAllWalkers();
513
514 if (WalkersActors.IsEmpty())
515 {
516 return;
517 }
518
519 for (AWalker* Walker : WalkersActors)
520 {
521 if (Walker)
522 {
523 Walker->Destroy();
524 }
525 }
526
527 WalkersActors.Empty();
528
529 FString Message = FString::Printf(TEXT("Destroyed all %d Walkers."), WalkersActors.Num());
530 SimulatorLog::Log(Message);
531}
532
534{
535 if (!VehicleDataAsset.IsValid())
536 {
537 VehicleDataAsset.Reset();
538 FSoftObjectPath VehicleActorAssetMap(VEHICLE_ASSET_PATH);
539 VehicleDataAsset = Cast<UVehicleActorAssetMapDataAsset>(VehicleActorAssetMap.TryLoad());
540 }
541}
542
544{
545 if (FoliageActorAssetMapDataAsset.IsValid())
546 {
547 return;
548 }
549
551 FSoftObjectPath FoliageActorAssetMapDataAssetPath(FOLIAGE_ASSET_PATH);
552 FoliageActorAssetMapDataAsset = Cast<UFoliageActorAssetMapDataAsset>(FoliageActorAssetMapDataAssetPath.TryLoad());
553
554 if (FoliageActorAssetMapDataAsset.IsValid())
555 {
557 FoliageTypeMap.Empty();
558 FoliageTypeMap = UEnumUtilities::CreateEnumStringMap<EFoliageTypes>("/Script/Agrarsense.EFoliageTypes");
559 }
560}
561
563{
564 if (PropActorAssetMapDataAsset.IsValid())
565 {
566 return;
567 }
568
569 FSoftObjectPath PropActorAssetMapDataAssetPath(PROP_ASSET_PATH);
570 PropActorAssetMapDataAsset = Cast<UPropActorAssetMapDataAsset>(PropActorAssetMapDataAssetPath.TryLoad());
571
572 if (PropActorAssetMapDataAsset.IsValid())
573 {
574 PropActorAssetMapDataAsset->Props.GetKeys(PropKeys);
575 SpawnedPropActors.Empty();
576
577 PropTypeMap.Empty();
578 PropTypeMap = UEnumUtilities::CreateEnumStringMap<EPropTypes>("/Script/Agrarsense.EPropTypes");
579 }
580}
581
583{
584 if (!WalkerActorAssetMapDataAsset.IsValid())
585 {
586 FSoftObjectPath WalkerActorAssetMapDataAssetPath(WALKER_ASSET_PATH);
587 WalkerActorAssetMapDataAsset = Cast<UWalkerActorAssetMapDataAsset>(WalkerActorAssetMapDataAssetPath.TryLoad());
588 }
589}
static const FString PROP_ASSET_PATH
static const FString VEHICLE_ASSET_PATH
static const FString FOLIAGE_ASSET_PATH
static const FString WALKER_ASSET_PATH
EFoliageTypes
Definition: FoliageTypes.h:15
EPropTypes
Definition: PropTypes.h:15
EVehicleTypes
Definition: VehicleTypes.h:15
void SetTypeAndModel(const FString &Type, const FString &Model)
Definition: Walker.h:30
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)
static TWeakObjectPtr< UFoliageActorAssetMapDataAsset > FoliageActorAssetMapDataAsset
Definition: AssetLibrary.h:185
static UWalkerActorAssetMapDataAsset * GetWalkerActorAssetMapDataAsset()
static TArray< AInstancedActor * > GetAllAddedPropActors()
static void SetupWalkerDataAsset()
static TArray< FInstancedActorData > SpawnedPropActors
Definition: AssetLibrary.h:192
static TMap< EFoliageTypes, FString > FoliageTypeMap
Definition: AssetLibrary.h:186
static AVehicle * SpawnVehicle(EVehicleTypes VehicleType, FTransform SpawnTransform, const FString &ActorName, const FString &ActorID, bool SnapAboveGround=false, float AboveOffset=150.0f, bool DestroyOverlappingActors=false)
static void DestroyAllWalkers()
static AActor * TrySpawnActorDeferred(TSubclassOf< AActor > ActorClass, FTransform &Transform, FString ActorName, FString ActorID, bool SnapToGround, bool RandomZRotation, ESpawnActorCollisionHandlingMethod CollisionMethod=ESpawnActorCollisionHandlingMethod::Undefined)
static TWeakObjectPtr< UVehicleActorAssetMapDataAsset > VehicleDataAsset
Definition: AssetLibrary.h:181
static void SetupVehicleDataAsset()
static EFoliageTypes GetRandomFoliageType()
static void DestroyOverlappingActorsSphere(AActor *SpawnedActor)
static TArray< FVehicleData > GetSpawnedVehicles()
static TArray< EPropTypes > PropKeys
Definition: AssetLibrary.h:194
static TArray< EFoliageTypes > FoliageKeys
Definition: AssetLibrary.h:188
static TMap< EPropTypes, FString > PropTypeMap
Definition: AssetLibrary.h:193
static TArray< AInstancedActor * > GetAllAddedFoliageActors()
static TArray< AWalker * > GetAllWalkers()
static void SetupPropDataAsset()
static TWeakObjectPtr< UPropActorAssetMapDataAsset > PropActorAssetMapDataAsset
Definition: AssetLibrary.h:191
static AActor * SpawnProp(EPropTypes PropType, FTransform Transform, FString ActorName="", FString ActorID="", bool RandomZRotation=false, bool SnapToGround=false)
static void SetupFoliageDataAsset()
static TWeakObjectPtr< UWalkerActorAssetMapDataAsset > WalkerActorAssetMapDataAsset
Definition: AssetLibrary.h:197
static AActor * TrySpawnActor(TSubclassOf< AActor > ActorClass, FTransform &Transform, FString ActorName, FString ActorID, bool SnapToGround, bool RandomZRotation, ESpawnActorCollisionHandlingMethod CollisionMethod=ESpawnActorCollisionHandlingMethod::Undefined)
static AWalker * SpawnWalker(FWalkerParameters Parameters, const FString &ActorName="", const FString &ActorID="", bool StartAutomatically=true)
static EPropTypes GetRandomPropType()
static TArray< FVehicleData > SpawnedVehicles
Definition: AssetLibrary.h:182
static TArray< TWeakObjectPtr< AWalker > > SpawnedWalkers
Definition: AssetLibrary.h:198
static UPropActorAssetMapDataAsset * GetPropActorAssetMapDataAsset()
static UFoliageActorAssetMapDataAsset * GetFoliageActorAssetMapDataAsset()
static AActor * SpawnFoliage(EFoliageTypes FoliageType, FTransform Transform, FString ActorName="", FString ActorID="", bool RandomZRotation=true, bool SnapToGround=true)
static TArray< FInstancedActorData > AddedFoliageActors
Definition: AssetLibrary.h:187
static FString ConvertVehicleTypeToString(EVehicleTypes VehicleType)
static FString ConvertWalkerTypeToString(EWalkerType WalkerType)
static bool SnapActorAboveGround(AActor *Actor, float AboveOffset=50.0f)
static bool SnapActorToGround(AActor *Actor, float StartZOffset=600.0f, float EndZOffset=600.0f)
TSubclassOf< AActor > Actor
Definition: ActorAsset.h:37
TWeakObjectPtr< AInstancedActor > Actor
EVehicleTypes VehicleType
Definition: AssetLibrary.h:46
TWeakObjectPtr< AVehicle > Vehicle
Definition: AssetLibrary.h:40
EWalkerAction WalkerAction
TArray< FTransform > Points
EWalkerType WalkerType