From 1501a13b1e5b15f2aaf34132466d983a354e2ca6 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Thu, 6 Apr 2023 17:01:39 +0200 Subject: [PATCH] Added spectator_as_ego settings --- LibCarla/source/carla/rpc/EpisodeSettings.h | 17 ++++++++++++----- PythonAPI/carla/source/libcarla/World.cpp | 4 +++- .../Source/Carla/Game/CarlaGameModeBase.cpp | 1 + .../Source/Carla/MapGen/LargeMapManager.cpp | 19 +++++++++++++++++-- .../Source/Carla/MapGen/LargeMapManager.h | 6 +++++- .../Carla/Source/Carla/Server/CarlaServer.cpp | 12 ++++++++++++ .../Source/Carla/Settings/EpisodeSettings.h | 4 +++- 7 files changed, 53 insertions(+), 10 deletions(-) diff --git a/LibCarla/source/carla/rpc/EpisodeSettings.h b/LibCarla/source/carla/rpc/EpisodeSettings.h index bdc6e37cb..af9a7b578 100644 --- a/LibCarla/source/carla/rpc/EpisodeSettings.h +++ b/LibCarla/source/carla/rpc/EpisodeSettings.h @@ -47,9 +47,11 @@ namespace rpc { float actor_active_distance = 2000.f; // 2km + float spectator_as_ego = true; + MSGPACK_DEFINE_ARRAY(synchronous_mode, no_rendering_mode, fixed_delta_seconds, substepping, max_substep_delta_time, max_substeps, max_culling_distance, deterministic_ragdolls, - tile_stream_distance, actor_active_distance); + tile_stream_distance, actor_active_distance, spectator_as_ego); // ========================================================================= // -- Constructors --------------------------------------------------------- @@ -67,7 +69,8 @@ namespace rpc { float max_culling_distance = 0.0f, bool deterministic_ragdolls = true, float tile_stream_distance = 3000.f, - float actor_active_distance = 2000.f) + float actor_active_distance = 2000.f, + bool spectator_as_ego = true) : synchronous_mode(synchronous_mode), no_rendering_mode(no_rendering_mode), fixed_delta_seconds( @@ -78,7 +81,8 @@ namespace rpc { max_culling_distance(max_culling_distance), deterministic_ragdolls(deterministic_ragdolls), tile_stream_distance(tile_stream_distance), - actor_active_distance(actor_active_distance) {} + actor_active_distance(actor_active_distance), + spectator_as_ego(spectator_as_ego) {} // ========================================================================= // -- Comparison operators ------------------------------------------------- @@ -95,7 +99,8 @@ namespace rpc { (max_culling_distance == rhs.max_culling_distance) && (deterministic_ragdolls == rhs.deterministic_ragdolls) && (tile_stream_distance == tile_stream_distance) && - (actor_active_distance == actor_active_distance); + (actor_active_distance == actor_active_distance) && + (spectator_as_ego == spectator_as_ego); } bool operator!=(const EpisodeSettings &rhs) const { @@ -119,7 +124,8 @@ namespace rpc { Settings.MaxCullingDistance, Settings.bDeterministicRagdolls, Settings.TileStreamingDistance, - Settings.ActorActiveDistance) { + Settings.ActorActiveDistance, + Settings.SpectatorAsEgo) { constexpr float CMTOM = 1.f/100.f; tile_stream_distance = CMTOM * Settings.TileStreamingDistance; actor_active_distance = CMTOM * Settings.ActorActiveDistance; @@ -140,6 +146,7 @@ namespace rpc { Settings.bDeterministicRagdolls = deterministic_ragdolls; Settings.TileStreamingDistance = MTOCM * tile_stream_distance; Settings.ActorActiveDistance = MTOCM * actor_active_distance; + Settings.SpectatorAsEgo = spectator_as_ego; return Settings; } diff --git a/PythonAPI/carla/source/libcarla/World.cpp b/PythonAPI/carla/source/libcarla/World.cpp index 00a9f7585..1d3b258c9 100644 --- a/PythonAPI/carla/source/libcarla/World.cpp +++ b/PythonAPI/carla/source/libcarla/World.cpp @@ -163,7 +163,8 @@ void export_world() { arg("max_culling_distance")=0.0f, arg("deterministic_ragdolls")=false, arg("tile_stream_distance")=3000.f, - arg("actor_active_distance")=2000.f))) + arg("actor_active_distance")=2000.f, + arg("spectator_as_ego")=true))) .def_readwrite("synchronous_mode", &cr::EpisodeSettings::synchronous_mode) .def_readwrite("no_rendering_mode", &cr::EpisodeSettings::no_rendering_mode) .def_readwrite("substepping", &cr::EpisodeSettings::substepping) @@ -181,6 +182,7 @@ void export_world() { }) .def_readwrite("tile_stream_distance", &cr::EpisodeSettings::tile_stream_distance) .def_readwrite("actor_active_distance", &cr::EpisodeSettings::actor_active_distance) + .def_readwrite("spectator_as_ego", &cr::EpisodeSettings::spectator_as_ego) .def("__eq__", &cr::EpisodeSettings::operator==) .def("__ne__", &cr::EpisodeSettings::operator!=) .def(self_ns::str(self_ns::self)) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp index a72a2fa7e..332b8ad07 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp @@ -224,6 +224,7 @@ void ACarlaGameModeBase::BeginPlay() if (LMManager) { LMManager->RegisterInitialObjects(); + LMManager->ConsiderSpectatorAsEgo(Episode->GetSettings().SpectatorAsEgo); } // Manually run begin play on lights as it may not run on sublevels diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/LargeMapManager.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/LargeMapManager.cpp index 5fe20ea5d..71ba50d08 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/LargeMapManager.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/LargeMapManager.cpp @@ -431,7 +431,7 @@ void ALargeMapManager::GenerateMap(FString InAssetsPath) GEngine->ForceGarbageCollection(true); ActorsToConsider.Reset(); - if (Spectator) + if (SpectatorAsEgo && Spectator) { ActorsToConsider.Add(Spectator); } @@ -738,7 +738,7 @@ void ALargeMapManager::RemovePendingActorsToRemove() { ActorsToConsider.Remove(ActorToRemove); } - if(ActorsToConsider.Num() == 0 && Spectator) + if(ActorsToConsider.Num() == 0 && SpectatorAsEgo && Spectator) { ActorsToConsider.Add(Spectator); } @@ -1118,3 +1118,18 @@ void ALargeMapManager::PrintMapInfo() } } } + +void ALargeMapManager::ConsiderSpectatorAsEgo(bool _SpectatorAsEgo) +{ + SpectatorAsEgo = _SpectatorAsEgo; + if(SpectatorAsEgo && ActorsToConsider.Num() == 0 && Spectator) + { + // Activating the spectator in an empty world + ActorsToConsider.Add(Spectator); + } + if (!SpectatorAsEgo && ActorsToConsider.Num() == 1 && ActorsToConsider.Contains(Spectator)) + { + // Deactivating the spectator in a world with no other egos + ActorsToConsider.Reset(); + } +} diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/LargeMapManager.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/LargeMapManager.h index 2b79ed2c7..570846ec4 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/LargeMapManager.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/MapGen/LargeMapManager.h @@ -202,6 +202,11 @@ public: ACarlaWheeledVehicle* GetHeroVehicle(); + // The spectator is treated as an ego vehicle by default when no other egos are around, + // but this can be changed from the Python API, which ends up in this function. + bool SpectatorAsEgo = false; + void ConsiderSpectatorAsEgo(bool _SpectatorAsEgo); + protected: void RemoveLandscapeCollisionIfHaveTerraMechanics(ULevel* InLevel); @@ -343,5 +348,4 @@ protected: UPROPERTY(VisibleAnywhere, Category = "Large Map Manager") bool bHasTerramechanics = false; - }; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp index 7d8d51edd..8c26b5bc6 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp @@ -539,6 +539,18 @@ void FCarlaServer::FPimpl::BindActions() REQUIRE_CARLA_EPISODE(); Episode->ApplySettings(settings); StreamingServer.SetSynchronousMode(settings.synchronous_mode); + + ACarlaGameModeBase* GameMode = UCarlaStatics::GetGameMode(Episode->GetWorld()); + if (!GameMode) + { + RESPOND_ERROR("unable to find CARLA game mode"); + } + ALargeMapManager* LargeMap = GameMode->GetLMManager(); + if (LargeMap) + { + LargeMap->ConsiderSpectatorAsEgo(settings.spectator_as_ego); + } + return FCarlaEngine::GetFrameCounter(); }; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/EpisodeSettings.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/EpisodeSettings.h index e78ea84d0..aa95b1b78 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/EpisodeSettings.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/EpisodeSettings.h @@ -35,6 +35,8 @@ struct CARLA_API FEpisodeSettings float TileStreamingDistance = 300000.f; // 3km - float ActorActiveDistance = 200000.f; // 3km + float ActorActiveDistance = 200000.f; // 2km + + float SpectatorAsEgo = true; };