From e574c727715d056a65e966671fd039d3839d5020 Mon Sep 17 00:00:00 2001
From: Rajat Singhal <rajatsinghal564@gmail.com>
Date: Sun, 7 Feb 2021 14:51:18 +0530
Subject: [PATCH] Store correct vehicle settings instead of default one

---
 AirLib/include/common/AirSimSettings.hpp          | 15 +++++++++++----
 .../Plugins/AirSim/Source/SimMode/SimModeBase.cpp |  6 +-----
 Unreal/Plugins/AirSim/Source/WorldSimApi.cpp      | 13 +++++--------
 3 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/AirLib/include/common/AirSimSettings.hpp b/AirLib/include/common/AirSimSettings.hpp
index 9aeb937ee3..2f01fb22c5 100644
--- a/AirLib/include/common/AirSimSettings.hpp
+++ b/AirLib/include/common/AirSimSettings.hpp
@@ -436,12 +436,19 @@ struct AirSimSettings {
     }
 
     // This is for the case when a new vehicle is made on the fly, at runtime
-    void addVehicleSetting(const VehicleSetting &vehicle_setting)
+    void addVehicleSetting(const std::string& vehicle_name, const std::string& vehicle_type, const Pose& pose, const std::string& pawn_path="")
     {
-        std::unique_ptr<VehicleSetting> vehicle_setting_p = std::unique_ptr<VehicleSetting>(new VehicleSetting());
+        auto vehicle_setting = std::unique_ptr<VehicleSetting>(new VehicleSetting());
 
-        // Usually we have a pointer to an entry from the json, but here we have to make a new one
-        vehicles[vehicle_setting.vehicle_name] = std::move(vehicle_setting_p);
+        vehicle_setting->vehicle_name = vehicle_name;
+        vehicle_setting->vehicle_type = vehicle_type;
+        vehicle_setting->position = pose.position;
+        vehicle_setting->pawn_path = pawn_path;
+
+        VectorMath::toEulerianAngle(pose.orientation, vehicle_setting->rotation.pitch, 
+                                    vehicle_setting->rotation.roll, vehicle_setting->rotation.yaw);
+
+        vehicles[vehicle_name] = std::move(vehicle_setting);
     }
 
     const VehicleSetting* getVehicleSetting(const std::string& vehicle_name) const
diff --git a/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp b/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp
index d3d2e31b9c..aa5f696441 100644
--- a/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp
+++ b/Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp
@@ -609,17 +609,13 @@ std::unique_ptr<PawnSimApi> ASimModeBase::createVehicleApi(APawn* vehicle_pawn)
     return vehicle_sim_api;
 }
 
-bool ASimModeBase::createVehicleAtRuntime(const AirSimSettings::VehicleSetting &vehicle_setting)
+bool ASimModeBase::createVehicleAtRuntime(const AirSimSettings::VehicleSetting& vehicle_setting)
 {
     if (!isVehicleTypeSupported(vehicle_setting.vehicle_type)) {
         Utils::log(Utils::stringf("Vehicle type %s is not supported in this game mode", vehicle_setting.vehicle_type.c_str()), Utils::kLogLevelWarn);
         return false;
     }
 
-    // Retroactively adjust AirSimSettings, so it's like we knew about this vehicle all along
-    // (Other places in the code use this for reference)
-    AirSimSettings::singleton().addVehicleSetting(vehicle_setting);
-
     auto spawned_pawn = createVehiclePawn(vehicle_setting);
 
     auto vehicle_sim_api = createVehicleApi(spawned_pawn);
diff --git a/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp b/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp
index 58ca6c6fe7..fdb7dfe100 100644
--- a/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp
+++ b/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp
@@ -270,21 +270,18 @@ void WorldSimApi::setTimeOfDay(bool is_enabled, const std::string& start_datetim
 
 bool WorldSimApi::createVehicleAtRuntime(const std::string& vehicle_name, const std::string& vehicle_type, const Pose& pose, const std::string& pawn_path)
 {
-    // Create settings object
-    AirSimSettings::VehicleSetting vehicle_setting;
+    // TODO: Figure out a better way to add more fields
+    //       Maybe allow passing a JSON string for the vehicle settings?
 
-    vehicle_setting.vehicle_name = vehicle_name;
-    vehicle_setting.vehicle_type = vehicle_type;
-    vehicle_setting.pawn_path = pawn_path;
-    vehicle_setting.position = pose.position;
-    VectorMath::toEulerianAngle(pose.orientation, vehicle_setting.rotation.pitch, vehicle_setting.rotation.roll, vehicle_setting.rotation.yaw);
+    // Retroactively adjust AirSimSettings, so it's like we knew about this vehicle all along
+    AirSimSettings::singleton().addVehicleSetting(vehicle_name, vehicle_type, pose, pawn_path);
 
     bool result;
 	
     // We need to run this code on the main game thread, since it iterates over actors
     FGraphEventRef task = FFunctionGraphTask::CreateAndDispatchWhenReady([&]()
     {
-        result = simmode_->createVehicleAtRuntime(vehicle_setting);
+        result = simmode_->createVehicleAtRuntime(*(AirSimSettings::singleton().getVehicleSetting(vehicle_name)));
     }, TStatId(), NULL, ENamedThreads::GameThread);
 
     // Wait for the result