diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/BaseMap/MainLargeMap/MapGeneratorBaseLargeMap.umap b/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/BaseMap/MainLargeMap/MapGeneratorBaseLargeMap.umap new file mode 100644 index 000000000..25ea52aff Binary files /dev/null and b/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/BaseMap/MainLargeMap/MapGeneratorBaseLargeMap.umap differ diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/BaseMap/MapGeneratorBaseMap.umap b/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/BaseMap/MapGeneratorBaseMap.umap deleted file mode 100644 index 6c1007c34..000000000 Binary files a/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/BaseMap/MapGeneratorBaseMap.umap and /dev/null differ diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/BaseMap/Tiles/MapGeneratorBaseMap.umap b/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/BaseMap/Tiles/MapGeneratorBaseMap.umap new file mode 100644 index 000000000..91a721149 Binary files /dev/null and b/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/BaseMap/Tiles/MapGeneratorBaseMap.umap differ diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/Textures/RT_HeightMap.uasset b/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/Textures/RT_HeightMap.uasset index 7d3592432..62309ed6c 100644 Binary files a/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/Textures/RT_HeightMap.uasset and b/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/Textures/RT_HeightMap.uasset differ diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/UWB_CARLA.uasset b/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/UWB_CARLA.uasset index dbfabc1fb..572396c05 100644 Binary files a/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/UWB_CARLA.uasset and b/Unreal/CarlaUE4/Plugins/CarlaTools/Content/MapGenerator/UWB_CARLA.uasset differ diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/MapGeneratorWidget.cpp b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/MapGeneratorWidget.cpp index 63cd34ef5..916dc88e5 100644 --- a/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/MapGeneratorWidget.cpp +++ b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/MapGeneratorWidget.cpp @@ -20,41 +20,52 @@ -FString UMapGeneratorWidget::GenerateMapFiles(const FString& destinationPath, const FString& mapName) +FString UMapGeneratorWidget::GenerateMapFiles(const FMapGeneratorMetaInfo& metaInfo) { DEBUG_MSG("Generating new map..."); FString errorMsg = ""; - FAssetData worldAssetData; - // 1. Creating Levels - bool bLoaded = LoadWorld(worldAssetData); - GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, bLoaded ? "Loaded CORRECT" : "NOT loaded"); + CreateMainLargeMap(metaInfo); + CreateTilesMaps(metaInfo); + // bool bLoaded = LoadWorld(worldAssetData); + // GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, bLoaded ? "Loaded CORRECT" : "NOT loaded"); // 2. Applying heightmap - UWorld* world = CastChecked(worldAssetData.GetAsset()); - ALandscape* landscape = (ALandscape*) UGameplayStatics::GetActorOfClass(world, ALandscape::StaticClass()); - GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, landscape!=nullptr ? "L TRUE" : "L FALSE"); - AssignLandscapeHeightMap(landscape); + // UWorld* world = CastChecked(worldAssetData.GetAsset()); + // ALandscape* landscape = (ALandscape*) UGameplayStatics::GetActorOfClass(world, ALandscape::StaticClass()); + // GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, landscape!=nullptr ? "L TRUE" : "L FALSE"); + // AssignLandscapeHeightMap(landscape); // 3. Saving world - bool bSaved = SaveWorld(worldAssetData, destinationPath, mapName); - GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, bSaved ? "Saved CORRECT" : "NOT saved"); + // bool bSaved = SaveWorld(worldAssetData, metaInfo.destinationPath, metaInfo.mapName); + // GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, bSaved ? "Saved CORRECT" : "NOT saved"); return errorMsg; } -bool UMapGeneratorWidget::LoadWorld(FAssetData& WorldAssetData) +bool UMapGeneratorWidget::LoadBaseTileWorld(FAssetData& WorldAssetData) +{ + const FString baseMapPath= TEXT("/CarlaTools/MapGenerator/BaseMap/Tiles"); + return LoadWorld(WorldAssetData, baseMapPath); +} + +bool UMapGeneratorWidget::LoadBaseLargeMapWorld(FAssetData& WorldAssetData) +{ + const FString baseMapPath= TEXT("/CarlaTools/MapGenerator/BaseMap/MainLargeMap"); + return LoadWorld(WorldAssetData, baseMapPath); +} + +bool UMapGeneratorWidget::LoadWorld(FAssetData& WorldAssetData, const FString& baseMapPath) { TArray AssetDatas; UObjectLibrary *MapObjectLibrary; - const FString BaseMap = TEXT("/CarlaTools/MapGenerator/BaseMap"); // Loading Map from folder using object library MapObjectLibrary = UObjectLibrary::CreateLibrary(UWorld::StaticClass(), false, GIsEditor); MapObjectLibrary->AddToRoot(); - MapObjectLibrary->LoadAssetDataFromPath(*BaseMap); + MapObjectLibrary->LoadAssetDataFromPath(*baseMapPath); MapObjectLibrary->LoadAssetsFromAssetData(); MapObjectLibrary->GetAssetDataList(AssetDatas); if (AssetDatas.Num() > 0) @@ -103,4 +114,51 @@ bool UMapGeneratorWidget::SaveWorld(FAssetData& WorldToBeSaved, const FString& D *PackageFileName, GError, nullptr, true, true, SAVE_NoError); } +bool UMapGeneratorWidget::CreateMainLargeMap(const FMapGeneratorMetaInfo& metaInfo) +{ + FAssetData worldAssetData; + bool bLoaded = LoadBaseLargeMapWorld(worldAssetData); + bool bSaved = SaveWorld(worldAssetData, metaInfo.destinationPath, metaInfo.mapName); + + return true; +} + +bool UMapGeneratorWidget::CreateTilesMaps(const FMapGeneratorMetaInfo& metaInfo) +{ + FAssetData worldAssetData; + + int numberOfTiles = metaInfo.sizeX * metaInfo.sizeY; + + for(int i = 0; i < numberOfTiles; i++) + { + bool bLoaded = LoadBaseTileWorld(worldAssetData); + + ApplyHeightMapToLandscape(worldAssetData); + + const FString mapName = metaInfo.mapName + "_" + FString::FromInt(i); + bool bSaved = SaveWorld(worldAssetData, metaInfo.destinationPath, mapName); + GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, bSaved ? "Saved CORRECT" : "NOT saved"); + } + return true; +} + +bool UMapGeneratorWidget::ApplyHeightMapToLandscape(FAssetData& worldAssetData) +{ + UWorld* world; + UObjectRedirector* BaseMapRedirector = Cast(worldAssetData.GetAsset()); + if(BaseMapRedirector != nullptr) + { + world = CastChecked(BaseMapRedirector->DestinationObject); + } + else + { + world = CastChecked(worldAssetData.GetAsset()); + } + ALandscape* landscape = (ALandscape*) UGameplayStatics::GetActorOfClass(world, ALandscape::StaticClass()); + GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, landscape!=nullptr ? "L TRUE" : "L FALSE"); + AssignLandscapeHeightMap(landscape); + return true; +} + + // #endif \ No newline at end of file diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/MapGeneratorWidget.h b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/MapGeneratorWidget.h index 92b7dd518..16db2a535 100644 --- a/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/MapGeneratorWidget.h +++ b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/MapGeneratorWidget.h @@ -17,6 +17,24 @@ #include "MapGeneratorWidget.generated.h" +USTRUCT(BlueprintType) +struct CARLATOOLS_API FMapGeneratorMetaInfo +{ + GENERATED_USTRUCT_BODY(); + + UPROPERTY(BlueprintReadWrite) + FString destinationPath; + + UPROPERTY(BlueprintReadWrite) + FString mapName; + + UPROPERTY(BlueprintReadWrite) + int sizeX; + + UPROPERTY(BlueprintReadWrite) + int sizeY; +}; + /// Class UMapGeneratorWidget extends the functionality of UEditorUtilityWidget /// to be able to generate and manage maps and largemaps tiles for procedural /// map generation @@ -25,6 +43,11 @@ class CARLATOOLS_API UMapGeneratorWidget : public UEditorUtilityWidget { GENERATED_BODY() + +private: + // UPROPERTY() + // UObjectLibrary *MapObjectLibrary; + public: /// This function invokes a blueprint event defined in widget blueprint /// event graph, which sets a heightmap to the @a landscape using @@ -33,26 +56,49 @@ public: UFUNCTION(BlueprintImplementableEvent) void AssignLandscapeHeightMap(ALandscape* landscape); -//private: - // UPROPERTY() - // UObjectLibrary *MapObjectLibrary; - -public: /// Function called by Widget Blueprint which generates all tiles of map /// @a mapName, and saves them in @a destinationPath /// Returns a void string is success and an error message if the process failed UFUNCTION(Category="Map Generator",BlueprintCallable) - FString GenerateMapFiles(const FString& destinationPath, const FString& mapName); + FString GenerateMapFiles(const FMapGeneratorMetaInfo& metaInfo); private: - /// Loads the base template UWorld object and is return in @a WorldAssetData + /// Loads the base tile map and stores it in @a WorldAssetData /// The funtions return true is success, otherwise false UFUNCTION() - bool LoadWorld(FAssetData& WorldAssetData); + bool LoadBaseTileWorld(FAssetData& WorldAssetData); + + /// Loads the base large map and stores it in @a WorldAssetData + /// The funtions return true is success, otherwise false + UFUNCTION() + bool LoadBaseLargeMapWorld(FAssetData& WorldAssetData); + + /// Loads the base template UWorld object from @a baseMapPath and returns + /// it in @a WorldAssetData + /// The funtions return true is success, otherwise false + UFUNCTION() + bool LoadWorld(FAssetData& WorldAssetData, const FString& baseMapPath); /// Saves a world contained in @a WorldToBeSaved, in the path defined in @a DestinationPath /// named as @a WorldName, as a package .umap UFUNCTION() bool SaveWorld(FAssetData& WorldToBeSaved, const FString& DestinationPath, const FString& WorldName); + + /// Takes the name of the map from @a metaInfo and created the main map + /// including all the actors needed by large map system + UFUNCTION() + bool CreateMainLargeMap(const FMapGeneratorMetaInfo& metaInfo); + + /// Takes @a metaInfo as input and generates all tiles based on the + /// dimensions specified for the map + /// The funtions return true is success, otherwise false + UFUNCTION() + bool CreateTilesMaps(const FMapGeneratorMetaInfo& metaInfo); + + /// Gets the landscape from the input world @a worldAssetData and + /// applies the heightmap to it + /// The funtions return true is success, otherwise false + UFUNCTION() + bool ApplyHeightMapToLandscape(FAssetData& worldAssetData); }; // #endif \ No newline at end of file