From 029cfc9943a68108100f37c6e5e122bbc97bc455 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Fri, 2 Jun 2017 21:24:43 +0100 Subject: [PATCH] Spawn vehicles based on config file --- Source/Carla/AI/VehicleSpawnerBase.cpp | 34 ++++++++++++------------- Source/Carla/AI/VehicleSpawnerBase.h | 27 ++++++++------------ Source/Carla/Game/CarlaGameModeBase.cpp | 11 ++++++++ Source/Carla/Game/CarlaGameModeBase.h | 8 ++++++ 4 files changed, 47 insertions(+), 33 deletions(-) diff --git a/Source/Carla/AI/VehicleSpawnerBase.cpp b/Source/Carla/AI/VehicleSpawnerBase.cpp index 364418dd5..e223116a4 100644 --- a/Source/Carla/AI/VehicleSpawnerBase.cpp +++ b/Source/Carla/AI/VehicleSpawnerBase.cpp @@ -11,18 +11,14 @@ // Sets default values AVehicleSpawnerBase::AVehicleSpawnerBase(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer), - RandomStream(Seed) -{ - PrimaryActorTick.bCanEverTick = true; - PrimaryActorTick.TickGroup = TG_PrePhysics; - -} - + RandomStream(Seed) {} void AVehicleSpawnerBase::BeginPlay() { Super::BeginPlay(); + NumberOfVehicles = FMath::Max(0, NumberOfVehicles); + // Allocate space for walkers. Vehicles.Reserve(NumberOfVehicles); @@ -38,22 +34,26 @@ void AVehicleSpawnerBase::BeginPlay() SpawnPoints.Add(*It); } - UE_LOG(LogCarla, Log, TEXT("Found %d positions for spawning vehilces"), SpawnPoints.Num()); + UE_LOG(LogCarla, Log, TEXT("Found %d positions for spawning vehicles"), SpawnPoints.Num()); - if (SpawnPoints.Num() < 2) { + if (SpawnPoints.Num() < NumberOfVehicles) { bSpawnVehicles = false; UE_LOG(LogCarla, Error, TEXT("We don't have enough spawn points for vehicles!")); } - while (bSpawnVehicles && (NumberOfVehicles > Vehicles.Num())) { - // Try to spawn one walker. - TryToSpawnRandomVehicle(); + if (bSpawnVehicles) { + const int32 MaximumNumberOfAttempts = 4 * NumberOfVehicles; + int32 NumberOfAttempts = 0; + while ((NumberOfVehicles > Vehicles.Num()) && (NumberOfAttempts < MaximumNumberOfAttempts)) { + // Try to spawn one vehicle. + TryToSpawnRandomVehicle(); + ++NumberOfAttempts; + } } -} -void AVehicleSpawnerBase::Tick(float DeltaTime) -{ - Super::Tick(DeltaTime); + if (NumberOfVehicles > Vehicles.Num()) { + UE_LOG(LogCarla, Error, TEXT("Requested %d vehicles, but we were only able to spawn %d"), NumberOfVehicles, Vehicles.Num()); + } } // ============================================================================= @@ -103,4 +103,4 @@ APlayerStart *AVehicleSpawnerBase::GetRandomSpawnPoint() const return (SpawnPoints.Num() > 0 ? SpawnPoints[RandomStream.RandRange(0, SpawnPoints.Num() - 1)] : nullptr); -} \ No newline at end of file +} diff --git a/Source/Carla/AI/VehicleSpawnerBase.h b/Source/Carla/AI/VehicleSpawnerBase.h index efc273438..8f0a62c06 100644 --- a/Source/Carla/AI/VehicleSpawnerBase.h +++ b/Source/Carla/AI/VehicleSpawnerBase.h @@ -12,18 +12,17 @@ class APlayerStart; UCLASS(Abstract) class CARLA_API AVehicleSpawnerBase : public AActor { - GENERATED_BODY() - - -public: - // Sets default values for this actor's properties - AVehicleSpawnerBase(const FObjectInitializer& ObjectInitializer); + GENERATED_BODY() +public: + // Sets default values for this actor's properties + AVehicleSpawnerBase(const FObjectInitializer& ObjectInitializer); protected: - // Called when the game starts or when spawned - virtual void BeginPlay() override; + + // Called when the game starts or when spawned + virtual void BeginPlay() override; UFUNCTION(BlueprintCallable) const FRandomStream &GetRandomStream() const @@ -40,11 +39,7 @@ protected: UFUNCTION(BlueprintImplementableEvent) AAICarlaVehicleController* GetVehicleController(AWheeledVehicle* Vechicle); - - -public: - // Called every frame - virtual void Tick(float DeltaTime) override; +public: void SetNumberOfVehicles(int32 Count); @@ -77,7 +72,7 @@ protected: UPROPERTY() FRandomStream RandomStream; - //UPROPERTY(Category = "Vechicle Spawner", VisibleAnywhere, AdvancedDisplay) + UPROPERTY(Category = "Vechicle Spawner", VisibleAnywhere, AdvancedDisplay) TArray SpawnPoints; /* UPROPERTY(Category = "Vechicle Spawner", BlueprintReadOnly, EditAnywhere, AdvancedDisplay) @@ -85,6 +80,6 @@ protected: */ UPROPERTY(Category = "Vehicle Spawner", BlueprintReadOnly, VisibleAnywhere, AdvancedDisplay) TArray< AWheeledVehicle *> Vehicles; - - + + }; diff --git a/Source/Carla/Game/CarlaGameModeBase.cpp b/Source/Carla/Game/CarlaGameModeBase.cpp index d52b3d5f8..c56f451d6 100644 --- a/Source/Carla/Game/CarlaGameModeBase.cpp +++ b/Source/Carla/Game/CarlaGameModeBase.cpp @@ -59,6 +59,10 @@ void ACarlaGameModeBase::InitGame( DynamicWeather = GetWorld()->SpawnActor(DynamicWeatherClass); } + if (VehicleSpawnerClass != nullptr) { + VehicleSpawner = GetWorld()->SpawnActor(VehicleSpawnerClass); + } + if (WalkerSpawnerClass != nullptr) { WalkerSpawner = GetWorld()->SpawnActor(WalkerSpawnerClass); } @@ -109,6 +113,13 @@ void ACarlaGameModeBase::BeginPlay() UE_LOG(LogCarla, Error, TEXT("Missing dynamic weather actor!")); } + // Setup other vehicles. + if (VehicleSpawner != nullptr) { + VehicleSpawner->SetNumberOfVehicles(CarlaSettings.NumberOfVehicles); + } else { + UE_LOG(LogCarla, Error, TEXT("Missing vehicle spawner actor!")); + } + // Setup walkers. if (WalkerSpawner != nullptr) { WalkerSpawner->SetNumberOfWalkers(CarlaSettings.NumberOfPedestrians); diff --git a/Source/Carla/Game/CarlaGameModeBase.h b/Source/Carla/Game/CarlaGameModeBase.h index 893c0ef9b..bb7788688 100644 --- a/Source/Carla/Game/CarlaGameModeBase.h +++ b/Source/Carla/Game/CarlaGameModeBase.h @@ -3,6 +3,7 @@ #pragma once #include "GameFramework/GameModeBase.h" +#include "AI/VehicleSpawnerBase.h" #include "AI/WalkerSpawnerBase.h" #include "CarlaGameControllerBase.h" #include "DynamicWeather.h" @@ -43,6 +44,10 @@ protected: UPROPERTY(Category = "CARLA Classes", EditAnywhere, BlueprintReadOnly) TSubclassOf DynamicWeatherClass; + /** The class of VehicleSpawner to spawn. */ + UPROPERTY(Category = "CARLA Classes", EditAnywhere, BlueprintReadOnly) + TSubclassOf VehicleSpawnerClass; + /** The class of WalkerSpawner to spawn. */ UPROPERTY(Category = "CARLA Classes", EditAnywhere, BlueprintReadOnly) TSubclassOf WalkerSpawnerClass; @@ -77,6 +82,9 @@ private: UPROPERTY() ADynamicWeather *DynamicWeather; + UPROPERTY() + AVehicleSpawnerBase *VehicleSpawner; + UPROPERTY() AWalkerSpawnerBase *WalkerSpawner; };