7 #include "Engine/WorldComposition.h" 8 #include "Engine/EngineTypes.h" 9 #include "Components/PrimitiveComponent.h" 10 #include "Landscape.h" 11 #include "LandscapeHeightfieldCollisionComponent.h" 12 #include "LandscapeComponent.h" 20 #include "FileHelper.h" 23 #define LARGEMAP_LOGS 1 26 #define LM_LOG(Level, Msg, ...) UE_LOG(LogCarla, Level, TEXT(Msg), ##__VA_ARGS__) 34 PrimaryActorTick.bCanEverTick =
true;
42 FCoreDelegates::PreWorldOriginOffset.RemoveAll(
this);
43 FCoreDelegates::PostWorldOriginOffset.RemoveAll(
this);
45 FWorldDelegates::LevelRemovedFromWorld.RemoveAll(
this);
46 FWorldDelegates::LevelAddedToWorld.RemoveAll(
this);
56 UWorld* World = GetWorld();
66 UWorldComposition* WorldComposition = World->WorldComposition;
67 WorldComposition->bRebaseOriginIn3DSpace =
true;
75 TArray<AActor*> FoundActors;
76 UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), FoundActors);
77 for(
auto CurrentActor : FoundActors)
79 if( CurrentActor->FindComponentByClass( UCustomTerrainPhysicsComponent::StaticClass() ) != nullptr )
87 APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
90 Spectator = PlayerController->GetPawnOrSpectator();
96 LM_LOG(Log,
"PreWorldOriginOffset Src: %s -> Dst: %s", *InSrcOrigin.ToString(), *InDstOrigin.ToString());
105 UWorld* World = GetWorld();
110 GEngine->AddOnScreenDebugMessage(66,
MsgTime, FColor::Yellow,
111 FString::Printf(TEXT(
"Src: %s -> Dst: %s"), *InSrcOrigin.ToString(), *InDstOrigin.ToString()));
112 LM_LOG(Log,
"PostWorldOriginOffset Src: %s -> Dst: %s", *InSrcOrigin.ToString(), *InDstOrigin.ToString());
115 const TArray<ULevelStreaming*>& StreamingLevels = World->GetStreamingLevels();
116 FColor LevelColor = FColor::White;
117 float MinDistance = 10000000.0f;
118 for (
const auto& TilePair :
MapTiles)
122 FVector LevelLocation = Tile.
Location;
123 float Distance = FVector::Dist(LevelLocation, FVector(InDstOrigin));
124 if (Distance < MinDistance)
126 MinDistance = Distance;
130 #endif // WITH_EDITOR 135 LM_LOG(Warning,
"OnLevelAddedToWorld");
144 LM_LOG(Warning,
"OnLevelRemovedFromWorld");
153 UWorld* World = GetWorld();
156 for (
const auto& CarlaActorPair : ActorRegistry)
158 if (CarlaActorPair.Value->GetActorInfo()->Description.Id ==
"spectator")
170 UWorld* World = GetWorld();
173 bool IsHeroVehicle =
false;
185 if(Attribute && (Attribute->
Value.Contains(
"hero") || Attribute->
Value.Contains(
"ego_vehicle")))
187 LM_LOG(Log,
"HERO VEHICLE DETECTED");
201 World->FlushLevelStreaming();
203 IsHeroVehicle =
true;
221 LM_LOG(Log,
"ACTIVE VEHICLE DETECTED");
229 LM_LOG(Log,
"DORMANT VEHICLE DETECTED");
237 LM_LOG(Warning,
"Actor Spawned at %s", *GlobalPosition.ToString());
255 LM_LOG(Warning,
"ALargeMapManager::OnActorDestroyed %s", *DestroyedActor->GetName());
257 UWorld* World = GetWorld();
315 InTransform.GetRotation(),
317 InTransform.GetScale3D());
328 InTransform.GetRotation(),
330 InTransform.GetScale3D());
342 Super::Tick(DeltaTime);
365 #endif // WITH_EDITOR 376 LM_LOG(Warning,
"Generating Map %s ...", *InAssetsPath);
382 TArray<FAssetData> AssetsData;
383 UObjectLibrary* ObjectLibrary = UObjectLibrary::CreateLibrary(UWorld::StaticClass(),
true,
true);
384 ObjectLibrary->LoadAssetDataFromPath(InAssetsPath);
385 ObjectLibrary->GetAssetDataList(AssetsData);
388 UWorld* World = GetWorld();
390 for (
const FAssetData& AssetData : AssetsData)
396 FString TileName = AssetData.AssetName.ToString();
397 if (!TileName.Contains(
"_Tile_"))
401 FString TileName_X =
"";
402 FString TileName_Y =
"";
403 size_t i = TileName.Len()-1;
405 TCHAR character = TileName[i];
406 if (character ==
'_') {
409 TileName_Y = FString::Chr(character) + TileName_Y;
413 TCHAR character = TileName[i];
414 if (character ==
'_') {
417 TileName_X = FString::Chr(character) + TileName_X;
419 FIntVector TileVectorID = FIntVector(FCString::Atoi(*TileName_X), FCString::Atoi(*TileName_Y), 0);
424 LoadCarlaMapTile(InAssetsPath +
"/" + AssetData.AssetName.ToString(), TileId);
426 ObjectLibrary->ConditionalBeginDestroy();
427 GEngine->ForceGarbageCollection(
true);
436 LM_LOG(Warning,
"GenerateMap num Tiles generated %d",
MapTiles.Num());
438 #endif // WITH_EDITOR 444 LM_LOG(Warning,
"Generating Large Map");
447 for (TPair<FString, FIntVector>& PathId : MapPathsIds)
449 FIntVector& TileVectorID = PathId.Value;
450 FString&
Path = PathId.Key;
456 LM_LOG(Warning,
"GenerateMap num Tiles generated %d",
MapTiles.Num());
457 #endif // WITH_EDITOR 467 UWorld* World = GetWorld();
468 UWorldComposition* WorldComposition = World->WorldComposition;
469 World->ClearStreamingLevels();
470 WorldComposition->TilesStreaming.Empty();
471 WorldComposition->GetTilesList().Empty();
475 ULevelStreamingDynamic* StreamingLevel = It.Value.StreamingLevel;
476 World->AddStreamingLevel(StreamingLevel);
477 WorldComposition->TilesStreaming.Add(StreamingLevel);
496 MinX = (TileID.X < MinX) ? TileID.X : MinX;
497 MaxX = (TileID.X > MaxX) ? TileID.X : MaxX;
499 MinY = (TileID.Y < MinY) ? TileID.Y : MinY;
500 MaxY = (TileID.Y > MaxY) ? TileID.Y : MaxY;
502 return { MaxX - MinX + 1, MaxY - MinY + 1, 0 };
514 LM_LOG(Warning,
"IsLevelOfTileLoaded Tile %s does not exist", *InTileID.ToString());
519 const ULevelStreamingDynamic* StreamingLevel = Tile->
StreamingLevel;
521 return (StreamingLevel && StreamingLevel->GetLoadedLevel());
526 FIntVector VectorId = FIntVector(
536 FIntVector VectorId = (
547 (int32)(TileID >> 32),
548 (int32)(TileID & (int32)(~0)),
561 TileVectorID.Y *= -1;
573 TileVectorID.
Y *= -1;
579 int64 X = ((int64)(TileVectorID.X) << 32);
580 int64 Y = (int64)(TileVectorID.Y) & 0x00000000FFFFFFFF;
602 ULevel* Level = StreamingLevel->GetLoadedLevel();
603 if (Level == InLevel)
633 NewTile.
Name = TileMapPath;
636 FVector TileLocation = NewTile.
Location;
637 FString TileName = NewTile.
Name;
638 UWorld* World = GetWorld();
639 UWorldComposition* WorldComposition = World->WorldComposition;
641 FString FullName = TileMapPath;
642 FString PackageFileName = FullName;
643 FString LongLevelPackageName = FPackageName::FilenameToLongPackageName(PackageFileName);
644 FString UniqueLevelPackageName = LongLevelPackageName;
646 ULevelStreamingDynamic* StreamingLevel = NewObject<ULevelStreamingDynamic>(World, *TileName);
647 check(StreamingLevel);
649 StreamingLevel->SetWorldAssetByPackageName(*UniqueLevelPackageName);
652 if (World->IsPlayInEditor())
654 FWorldContext WorldContext = GEngine->GetWorldContextFromWorldChecked(World);
655 StreamingLevel->RenameForPIE(WorldContext.PIEInstance);
657 StreamingLevel->SetShouldBeVisibleInEditor(
true);
658 StreamingLevel->LevelColor = FColor::MakeRandomColor();
659 #endif // WITH_EDITOR 661 StreamingLevel->SetShouldBeLoaded(
false);
662 StreamingLevel->SetShouldBeVisible(
false);
664 StreamingLevel->bInitiallyLoaded =
false;
665 StreamingLevel->bInitiallyVisible =
false;
666 StreamingLevel->LevelTransform = FTransform(TileLocation);
667 StreamingLevel->PackageNameToLoad = *FullName;
669 if (!FPackageName::DoesPackageExist(FullName, NULL, &PackageFileName))
671 LM_LOG(Error,
"Level does not exist in package with FullName variable -> %s", *FullName);
674 if (!FPackageName::DoesPackageExist(LongLevelPackageName, NULL, &PackageFileName))
676 LM_LOG(Error,
"Level does not exist in package with LongLevelPackageName variable -> %s", *LongLevelPackageName);
680 StreamingLevel->PackageNameToLoad = *LongLevelPackageName;
685 return MapTiles.Add(TileId, NewTile);
691 TSet<TileID> TilesToConsider;
707 TSet<TileID> TilesToBeVisible;
708 TSet<TileID> TilesToHidde;
735 ActorsToRemove.Reset();
742 ActivesToRemove.Reset();
748 DormantsToRemove.Reset();
754 UWorld* World = GetWorld();
763 FVector RelativeLocation = Actor->GetActorLocation();
776 FVector HeroLocation = HeroActor->GetActorLocation();
778 float DistanceSquared = (RelativeLocation - HeroLocation).SizeSquared();
790 LM_LOG(Warning,
"CheckActiveActors Actor does not exist -> Remove actor");
799 UWorld* World = GetWorld();
809 LM_LOG(Warning,
"Converting Active To Dormant... %d", Id);
815 ActiveToDormantActors.Reset();
821 UWorld* World = GetWorld();
832 LM_LOG(Log,
"CheckDormantActors Carla Actor %d not found", Id);
838 LM_LOG(Warning,
"CheckDormantActors IDs doesn't match!! Wanted = %d Received = %d", Id, CarlaActor->
GetActorId());
844 LM_LOG(Warning,
"CheckDormantActors Carla Actor %d is not dormant", Id);
853 FVector HeroLocation =
Actor->GetActorLocation();
858 float DistanceSquared = (RelativeLocation - HeroLocation).SizeSquared();
873 UWorld* World = GetWorld();
878 LM_LOG(Warning,
"Converting %d Dormant To Active", Id);
885 LM_LOG(Warning,
"Spawning dormant at %s\n\tOrigin: %s\n\tRel. location: %s", \
888 *((View->
GetActor()->GetActorLocation()).ToString()) \
894 LM_LOG(Warning,
"Actor %d could not be woken up, keeping sleep state", Id);
898 DormantToActiveActors.Reset();
906 UWorld* World = GetWorld();
907 UWorldComposition* WorldComposition = World->WorldComposition;
912 FVector ActorLocation = ActorToConsider->GetActorLocation();
913 FIntVector ILocation = FIntVector(ActorLocation.X, ActorLocation.Y, ActorLocation.Z);
918 World->SetNewWorldOrigin(FIntVector(NewOrigin));
925 TSet<TileID>& OutTilesToConsider)
928 check(ActorToConsider);
940 for (
int Y = UpperTileId.Y; Y <= LowerTileId.Y; Y++)
942 for (
int X = LowerTileId.X; X <= UpperTileId.X; X++)
946 FIntVector TileToCheck = FIntVector(X, Y, 0);
956 OutTilesToConsider.Add(TileID);
962 const TSet<TileID>& InTilesToConsider,
963 TSet<TileID>& OutTilesToBeVisible,
964 TSet<TileID>& OutTilesToHidde)
972 const TSet<TileID>& InTilesToUpdate,
973 bool InShouldBlockOnLoad,
974 bool InShouldBeLoaded,
975 bool InShouldBeVisible)
978 UWorld* World = GetWorld();
979 UWorldComposition* WorldComposition = World->WorldComposition;
986 ULevelStreamingDynamic* StreamingLevel = CarlaTile->
StreamingLevel;
987 StreamingLevel->bShouldBlockOnLoad = InShouldBlockOnLoad;
988 StreamingLevel->SetShouldBeLoaded(InShouldBeLoaded);
989 StreamingLevel->SetShouldBeVisible(InShouldBeVisible);
994 const TSet<TileID>& InTilesToBeVisible,
995 const TSet<TileID>& InTilesToHidde)
1011 int32 X = (int32)(TileID >> 32);
1012 int32 Y = (int32)(TileID);
1013 return FString::Printf(TEXT(
"Tile_%d_%d"), X, Y);
1018 int32 X = (int32)(TileID >> 32);
1019 int32 Y = (int32)(TileID);
1020 return FString::Printf(TEXT(
"%d_%d"), X, Y);
1025 FString FileContent =
"";
1026 FileContent += FString::Printf(TEXT(
"LargeMapManager state\n"));
1028 FileContent += FString::Printf(TEXT(
"Tile:\n"));
1029 FileContent += FString::Printf(TEXT(
"ID\tName\tLocation\n"));
1033 FileContent += FString::Printf(TEXT(
" %ld\t%s\t%s\n"), It.Key, *Tile.
Name, *Tile.
Location.ToString());
1035 FileContent += FString::Printf(TEXT(
"\nNum generated tiles: %d\n"), MapTiles.Num());
1038 TArray<FString> StringArray;
1039 AssetsPath.ParseIntoArray(StringArray, TEXT(
"/"),
false);
1041 FString FilePath = FPaths::ProjectSavedDir() + StringArray[StringArray.Num() - 1] +
".txt";
1042 FFileHelper::SaveStringToFile(
1045 FFileHelper::EEncodingOptions::AutoDetect,
1047 EFileWrite::FILEWRITE_Silent);
1052 UWorld* World = GetWorld();
1058 const TArray<FLevelCollection>& WorldLevelCollections = World->GetLevelCollections();
1059 const FLevelCollection* LevelCollection = World->GetActiveLevelCollection();
1060 const TArray<ULevel*>& Levels = World->GetLevels();
1061 const TArray<ULevelStreaming*>& StreamingLevels = World->GetStreamingLevels();
1062 ULevel* CurrentLevel = World->GetCurrentLevel();
1064 FString Output =
"";
1065 Output += FString::Printf(TEXT(
"Num levels in world composition: %d\n"), World->WorldComposition->TilesStreaming.Num());
1066 Output += FString::Printf(TEXT(
"Num levels loaded: %d\n"), Levels.Num() );
1067 Output += FString::Printf(TEXT(
"Num tiles loaded: %d\n"),
CurrentTilesLoaded.Num() );
1068 Output += FString::Printf(TEXT(
"Tiles loaded: [ "));
1071 Output += FString::Printf(TEXT(
"%s, "), *
TileIDToString(TileId));
1073 Output += FString::Printf(TEXT(
"]\n"));
1074 GEngine->AddOnScreenDebugMessage(0,
MsgTime, FColor::Cyan, Output);
1077 GEngine->AddOnScreenDebugMessage(LastMsgIndex++,
MsgTime, FColor::White,
1078 FString::Printf(TEXT(
"\nActor Global Position: %s km"), *(
FDVector(CurrentActorPosition) / (1000.0 * 100.0)).ToString()) );
1080 GEngine->AddOnScreenDebugMessage(LastMsgIndex++,
MsgTime, FColor::White, TEXT(
"Closest tiles - Distance:"));
1082 for (
const auto& TilePair :
MapTiles)
1086 FVector LevelLocation = Tile.
Location;
1087 float Distance =
FDVector::Dist(LevelLocation, CurrentActorPosition);
1089 FColor MsgColor = FColor::Green;
1092 GEngine->AddOnScreenDebugMessage(LastMsgIndex++,
MsgTime, MsgColor,
1093 FString::Printf(TEXT(
"%s %.2f"), *Level->GetName(), Distance / (1000.0f * 100.0f)));
1098 GEngine->AddOnScreenDebugMessage(LastMsgIndex++,
MsgTime, FColor::White,
1100 GEngine->AddOnScreenDebugMessage(LastMsgIndex++,
MsgTime, FColor::White,
1101 FString::Printf(TEXT(
"Num active actors (%d)"),
ActiveActors.Num()) );
1102 GEngine->AddOnScreenDebugMessage(LastMsgIndex++,
MsgTime, FColor::White,
1103 FString::Printf(TEXT(
"Num dormant actors (%d)"),
DormantActors.Num()));
1104 GEngine->AddOnScreenDebugMessage(LastMsgIndex++,
MsgTime, FColor::White,
1111 float ToKm = 1000.0f * 100.0f;
1112 FVector TileActorLocation =
Actor->GetActorLocation();
1115 Output += FString::Printf(TEXT(
"Local Loc: %s meters\n"), *(TileActorLocation / ToKm).ToString());
1116 Output += FString::Printf(TEXT(
"Client Loc: %s km\n"), *(ClientActorLocation / ToKm).ToString());
1117 Output +=
"---------------";
FCarlaActor * FindCarlaActor(FCarlaActor::IdType ActorId)
Find a Carla actor by id.
TileID GetTileID(FVector TileLocation) const
From a given location it retrieves the TileID that covers that area.
TSet< uint64 > CurrentTilesLoaded
FString GenerateTileName(TileID TileID)
void UpdateCurrentTilesLoaded(const TSet< TileID > &InTilesToBeVisible, const TSet< TileID > &InTilesToHidde)
A registry of all the Carla actors.
const int32 MaxTilesDistMsgIndex
void GenerateMap(FString InAssetsPath)
TSet< FCarlaActor::IdType > DormantsToRemove
TSet< FCarlaActor::IdType > DormantToActiveActors
bool ShouldTilesBlockOnLoad
void ConvertActiveToDormantActors()
static double Dist(const FDVector &V1, const FDVector &V2)
FIntVector GetNumTilesInXY() const
float LayerStreamingDistance
FVector GlobalToLocalLocation(const FVector &InLocation) const
void ConvertDormantToActiveActors()
float LayerStreamingDistanceSquared
ACarlaWheeledVehicle * GetHeroVehicle()
FCarlaMapTile & GetCarlaMapTile(FVector Location)
void WakeActorUp(carla::rpc::ActorId ActorId)
std::vector< cg::Location > Path
void RegisterTilesInWorldComposition()
TSet< FCarlaActor::IdType > ActiveToDormantActors
const int32 TilesDistMsgIndex
FCarlaMapTile & LoadCarlaMapTile(FString TileMapPath, TileID TileId)
FString TileIDToString(TileID TileID)
bool IsLevelOfTileLoaded(FIntVector InTileID) const
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
float ActorStreamingDistance
void PutActorToSleep(carla::rpc::ActorId ActorId)
void OnActorSpawned(const FCarlaActor &CarlaActor)
FIntVector CurrentOriginInt
static T Get(carla::rpc::Response< T > &response)
void UpdateTileState(const TSet< TileID > &InTilesToUpdate, bool InShouldBlockOnLoad, bool InShouldBeLoaded, bool InShouldBeVisible)
virtual void BeginPlay() override
float ActorStreamingDistanceSquared
FVector ToFVector() const
static void TagActorsInLevel(UWorld &World, bool bTagForSemanticSegmentation)
Set the tag of every actor in level.
const FActorRegistry & GetActorRegistry() const
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.
FActorData * GetActorData()
carla::SharedPtr< cc::Actor > Actor
void ClearWorldAndTiles()
void GetTilesThatNeedToChangeState(const TSet< TileID > &InTilesToConsider, TSet< TileID > &OutTilesToBeVisible, TSet< TileID > &OutTilesToHidde)
float tick_execution_time
const int32 ClientLocMsgIndex
void SetActorStreamingDistance(float Distance)
void PreWorldOriginOffset(UWorld *InWorld, FIntVector InSrcOrigin, FIntVector InDstOrigin)
void SetTileSize(float Size)
void DumpTilesTable() const
void RegisterInitialObjects()
A description of a Carla Actor with all its variation.
FActorDescription Description
const FActorInfo * GetActorInfo() const
TMap< uint64, FCarlaMapTile > MapTiles
void CheckIfRebaseIsNeeded()
void SetLayerStreamingDistance(float Distance)
void SetCurrentMapOrigin(const FIntVector &NewOrigin)
void GetTilesToConsider(const AActor *ActorToConsider, TSet< TileID > &OutTilesToConsider)
FDVector GetTileLocationD(TileID TileID) const
FTransform LocalToGlobalTransform(const FTransform &InTransform) const
TArray< AActor * > ActorsToConsider
void AddActorToUnloadedList(const FCarlaActor &CarlaActor, const FTransform &Transform)
#define LM_LOG(Level, Msg,...)
float GetActorStreamingDistance() const
float GetLayerStreamingDistance() const
FTransform GlobalToLocalTransform(const FTransform &InTransform) const
void PostWorldOriginOffset(UWorld *InWorld, FIntVector InSrcOrigin, FIntVector InDstOrigin)
static UCarlaEpisode * GetCurrentEpisode(const UObject *WorldContextObject)
void OnLevelRemovedFromWorld(ULevel *InLevel, UWorld *InWorld)
A view over an actor and its properties.
TSet< FCarlaActor::IdType > ActivesToRemove
TSet< AActor * > ActorsToRemove
float RebaseOriginDistance
const int32 MaxClientLocMsgIndex
IdType GetActorId() const
ULevelStreamingDynamic * StreamingLevel
void RemovePendingActorsToRemove()
void SetTile0Offset(const FVector &Offset)
TArray< FCarlaActor::IdType > ActiveActors
Base class for CARLA wheeled vehicles.
void OnActorDestroyed(AActor *DestroyedActor)
An actor attribute, may be an intrinsic (non-modifiable) attribute of the actor or an user-defined ac...
void Tick(float DeltaTime) override
float RebaseOriginDistanceSquared
FVector GetTileLocation(TileID TileID) const
geom::Transform Transform
FVector LocalToGlobalLocation(const FVector &InLocation) const
bool IsTileLoaded(TileID TileId) const
void CheckDormantActors()
ActorType GetActorType() const
FIntVector GetTileVectorID(FVector TileLocation) const
A view over an actor and its properties.
TArray< FCarlaActor::IdType > DormantActors
void OnLevelAddedToWorld(ULevel *InLevel, UWorld *InWorld)