Drafting CarlaServer behaviour

This commit is contained in:
nsubiron 2017-03-14 18:31:09 +01:00
parent 19a0e63500
commit 004e2cc4c7
8 changed files with 129 additions and 21 deletions

View File

@ -3,24 +3,130 @@
#include "Carla.h"
#include "CarlaGameController.h"
CarlaGameController::CarlaGameController()
#include "GameFramework/PlayerStart.h"
#include "CarlaVehicleController.h"
// -----------------------------------------------------------------------------
#include <vector>
struct Position {
float x;
float y;
};
struct Scene_Values {
std::vector<Position> _possible_Positions;
};
class CarlaServer {
public:
void sendWorld() {}
bool tryReadSceneInit(int &mode, int &scene) { mode = 0; return true; }
void sendSceneValues(const Scene_Values &) {}
bool tryReadEpisodeStart(size_t &start_index, size_t &end_index) { start_index = 0u; return true; }
void sendEndReset() {}
};
// =============================================================================
// -- static local methods -----------------------------------------------------
// =============================================================================
static void ReadSceneInit(CarlaServer &Server)
{
bCanEverTick = true;
int mode;
int scene;
while (!Server.tryReadSceneInit(mode, scene)) {
// wait.
}
}
static void SendSceneValues(CarlaServer &Server, const TArray<FTransform> &Transforms)
{
Scene_Values sceneValues;
sceneValues._possible_Positions.reserve(Transforms.Num());
for (const FTransform &Transform : Transforms) {
const FVector &Location = Transform.GetLocation();
sceneValues._possible_Positions.push_back({Location.X, Location.Y});
}
Server.sendSceneValues(sceneValues);
}
static size_t ReadEpisodeStart(CarlaServer &Server)
{
size_t StartIndex;
size_t EndIndex;
while (!Server.tryReadEpisodeStart(StartIndex, EndIndex)) {
// wait.
}
return StartIndex;
}
static bool TryReadEpisodeStart(CarlaServer &Server, size_t &StartIndex)
{
size_t EndIndex;
return Server.tryReadEpisodeStart(StartIndex, EndIndex);
}
static void SendReward(CarlaServer &Server, ACarlaVehicleController &Player)
{
}
static void ReadControl(CarlaServer &Server, ACarlaVehicleController &Player)
{
}
// =============================================================================
// -- CarlaGameController ------------------------------------------------------
// =============================================================================
CarlaGameController::CarlaGameController() :
Server(MakeUnique<CarlaServer>()),
Player(nullptr)
{
Server->sendWorld();
}
APlayerStart *CarlaGameController::ChoosePlayerStart(
const TArray<APlayerStart *> &AvailableStartSpots)
{
UE_LOG(LogCarla, Error, TEXT("Not implemented"));
if (AvailableStartTransforms.Num() == 0) {
AvailableStartTransforms.Reserve(AvailableStartSpots.Num());
for (APlayerStart *StartSpot : AvailableStartSpots) {
AvailableStartTransforms.Add(StartSpot->GetActorTransform());
}
}
return AvailableStartSpots[0u];
}
void CarlaGameController::RegisterPlayer(AController *NewPlayer)
{
UE_LOG(LogCarla, Error, TEXT("Not implemented"));
Player = Cast<ACarlaVehicleController>(NewPlayer);
check(Player != nullptr);
}
void CarlaGameController::Tick(float DeltaSeconds)
{
UE_LOG(LogCarla, Error, TEXT("Not implemented"));
check(Player != nullptr);
size_t StartIndex;
if (bIsResetting) {
// Resetting the world.
ReadSceneInit(*Server);
SendSceneValues(*Server, AvailableStartTransforms);
StartIndex = ReadEpisodeStart(*Server);
Player->SetActorTransform(AvailableStartTransforms[StartIndex]);
Server->sendEndReset();
bIsResetting = false;
} else if (TryReadEpisodeStart(*Server, StartIndex)) {
// Handle request for resetting the world.
Player->SetActorTransform(AvailableStartTransforms[StartIndex]);
Server->sendEndReset();
} else {
// Regular tick.
SendReward(*Server, *Player);
ReadControl(*Server, *Player);
}
}

View File

@ -4,6 +4,9 @@
#include "CarlaGameControllerBase.h"
class CarlaServer;
class ACarlaVehicleController;
/// Implements remote control of game and player.
class CARLA_API CarlaGameController : public CarlaGameControllerBase
{
@ -16,4 +19,14 @@ public:
virtual void RegisterPlayer(AController *NewPlayer) override;
virtual void Tick(float DeltaSeconds) override;
private:
TUniquePtr<CarlaServer> Server;
ACarlaVehicleController *Player;
TArray<FTransform> AvailableStartTransforms;
bool bIsResetting = true;
};

View File

@ -14,18 +14,9 @@ public:
virtual ~CarlaGameControllerBase() {}
bool CanEverTick() const
{
return bCanEverTick;
}
virtual APlayerStart *ChoosePlayerStart(const TArray<APlayerStart *> &AvailableStartSpots) = 0;
virtual void RegisterPlayer(AController *NewPlayer) = 0;
virtual void Tick(float DeltaSeconds) {}
protected:
bool bCanEverTick = false;
};

View File

@ -21,6 +21,7 @@ ACarlaGameMode::ACarlaGameMode() :
PlayerStateClass = ACarlaPlayerState::StaticClass();
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.TickGroup = TG_PrePhysics;
}
void ACarlaGameMode::InitGame(

View File

@ -38,6 +38,9 @@ ACarlaVehicleController::ACarlaVehicleController() :
PlayerCamera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
PlayerCamera->bUsePawnControlRotation = false;
PlayerCamera->FieldOfView = 90.f;
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.TickGroup = TG_PrePhysics;
}
ACarlaVehicleController::~ACarlaVehicleController() {}

View File

@ -5,6 +5,7 @@
#include "GameFramework/PlayerController.h"
#include "CarlaVehicleController.generated.h"
class ACarlaPlayerState;
class UCameraComponent;
class USpringArmComponent;
class UWheeledVehicleMovementComponent;

View File

@ -3,11 +3,6 @@
#include "Carla.h"
#include "MockGameController.h"
MockGameController::MockGameController()
{
bCanEverTick = false;
}
APlayerStart *MockGameController::ChoosePlayerStart(
const TArray<APlayerStart *> &AvailableStartSpots)
{

View File

@ -9,8 +9,6 @@ class CARLA_API MockGameController : public CarlaGameControllerBase
{
public:
MockGameController();
virtual APlayerStart *ChoosePlayerStart(const TArray<APlayerStart *> &AvailableStartSpots) override;
virtual void RegisterPlayer(AController *NewPlayer) override;