<commit_before>#include "../Flare.h"
#include "FlareGame.h"
#include "FlareSimulatedSector.h"
#include "FlareSector.h"
#include "FlareCollider.h"
#include "../Spacecrafts/FlareShell.h"
#include "../Spacecrafts/FlareSpacecraft.h"
#include "../Player/FlarePlayerController.h"


/*----------------------------------------------------
	Constructor
----------------------------------------------------*/

UFlareSector::UFlareSector(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	SectorRepartitionCache = false;
	IsDestroyingSector = false;
}

/*----------------------------------------------------
  Save
----------------------------------------------------*/

void UFlareSector::Load(UFlareSimulatedSector* Parent)
{
	DestroySector();
	ParentSector = Parent;
	LocalTime = Parent->GetData()->LocalTime;

	// Load asteroids
	for (int i = 0 ; i < ParentSector->GetData()->AsteroidData.Num(); i++)
	{
		LoadAsteroid(ParentSector->GetData()->AsteroidData[i]);
	}

	// Load safe location spacecrafts
	for (int i = 0 ; i < ParentSector->GetSectorSpacecrafts().Num(); i++)
	{
		UFlareSimulatedSpacecraft* Spacecraft = ParentSector->GetSectorSpacecrafts()[i];
		if (Spacecraft->GetData().SpawnMode == EFlareSpawnMode::Safe && (!Spacecraft->IsReserve() ||  Parent->GetGame()->GetPC()->GetPlayerShip() == Spacecraft))
		{
			LoadSpacecraft(Spacecraft);
		}
	}

	SectorRepartitionCache = false;

	// Load unsafe location spacecrafts
	for (int i = 0; i < ParentSector->GetSectorSpacecrafts().Num(); i++)
	{
		UFlareSimulatedSpacecraft* Spacecraft = ParentSector->GetSectorSpacecrafts()[i];
		if (Spacecraft->GetData().SpawnMode != EFlareSpawnMode::Safe && (!Spacecraft->IsReserve() ||  Parent->GetGame()->GetPC()->GetPlayerShip() == Spacecraft))
		{
			LoadSpacecraft(Spacecraft);
		}
	}

	// Check docking once all spacecraft are loaded
	for (int i = 0; i < SectorSpacecrafts.Num(); i++)
	{
		SectorSpacecrafts[i]->Redock();
	}

	// Load bombs
	for (int i = 0; i < ParentSector->GetData()->BombData.Num(); i++)
	{
		LoadBomb(ParentSector->GetData()->BombData[i]);
	}
}

void UFlareSector::Save()
{
	FFlareSectorSave* SectorData  = GetSimulatedSector()->GetData();

	SectorData->BombData.Empty();
	SectorData->AsteroidData.Empty();


	for (int i = 0 ; i < SectorBombs.Num(); i++)
	{
		SectorData->BombData.Add(*SectorBombs[i]->Save());
	}

	for (int i = 0 ; i < SectorAsteroids.Num(); i++)
	{
		SectorData->AsteroidData.Add(*SectorAsteroids[i]->Save());
	}

	SectorData->LocalTime = LocalTime + GetGame()->GetPlanetarium()->GetSmoothTime();
}

void UFlareSector::DestroySector()
{
	FLOG("UFlareSector::DestroySector");

	IsDestroyingSector = true;

	// Remove spacecrafts from world
	for (int SpacecraftIndex = 0 ; SpacecraftIndex < SectorSpacecrafts.Num(); SpacecraftIndex++)
	{
		SectorSpacecrafts[SpacecraftIndex]->Destroy();
	}

	for (int BombIndex = 0 ; BombIndex < SectorBombs.Num(); BombIndex++)
	{
		SectorBombs[BombIndex]->Destroy();
	}

	for (int AsteroidIndex = 0 ; AsteroidIndex < SectorAsteroids.Num(); AsteroidIndex++)
	{
		SectorAsteroids[AsteroidIndex]->Destroy();
	}

	for (int ShellIndex = 0 ; ShellIndex < SectorShells.Num(); ShellIndex++)
	{
		SectorShells[ShellIndex]->Destroy();
	}

	SectorSpacecrafts.Empty();
	SectorShips.Empty();
	SectorStations.Empty();
	SectorBombs.Empty();
	SectorAsteroids.Empty();
	SectorShells.Empty();

	IsDestroyingSector = false;
}


/*----------------------------------------------------
	Gameplay
----------------------------------------------------*/

AFlareAsteroid* UFlareSector::LoadAsteroid(const FFlareAsteroidSave& AsteroidData)
{
    FActorSpawnParameters Params;
    Params.bNoFail = true;
	Params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;

	AFlareAsteroid* Asteroid = GetGame()->GetWorld()->SpawnActor<AFlareAsteroid>(AFlareAsteroid::StaticClass(), AsteroidData.Location, AsteroidData.Rotation, Params);
    Asteroid->Load(AsteroidData);

	// TODO Check double add
	SectorAsteroids.Add(Asteroid);
    return Asteroid;
}

AFlareSpacecraft* UFlareSector::LoadSpacecraft(UFlareSimulatedSpacecraft* ParentSpacecraft)
{
	AFlareSpacecraft* Spacecraft = NULL;
	/*FLOGV("UFlareSector::LoadSpacecraft : Start loading ('%s')", *ParentSpacecraft->GetImmatriculation().ToString());*/

	// Spawn parameters
	FActorSpawnParameters Params;
	Params.bNoFail = true;
	Params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;

	// Create and configure the ship
	Spacecraft = GetGame()->GetWorld()->SpawnActor<AFlareSpacecraft>(ParentSpacecraft->GetDescription()->Template->GeneratedClass, ParentSpacecraft->GetData().Location, ParentSpacecraft->GetData().Rotation, Params);
	if (Spacecraft && !Spacecraft->IsPendingKillPending())
	{
		Spacecraft->Load(ParentSpacecraft);<commit_msg>#629 Added checks and logs<commit_after>#include "../Flare.h"
#include "FlareGame.h"
#include "FlareSimulatedSector.h"
#include "FlareSector.h"
#include "FlareCollider.h"
#include "../Spacecrafts/FlareShell.h"
#include "../Spacecrafts/FlareSpacecraft.h"
#include "../Player/FlarePlayerController.h"


/*----------------------------------------------------
	Constructor
----------------------------------------------------*/

UFlareSector::UFlareSector(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	SectorRepartitionCache = false;
	IsDestroyingSector = false;
}

/*----------------------------------------------------
  Save
----------------------------------------------------*/

void UFlareSector::Load(UFlareSimulatedSector* Parent)
{
	DestroySector();
	ParentSector = Parent;
	LocalTime = Parent->GetData()->LocalTime;

	// Load asteroids
	for (int i = 0 ; i < ParentSector->GetData()->AsteroidData.Num(); i++)
	{
		LoadAsteroid(ParentSector->GetData()->AsteroidData[i]);
	}

	// Load safe location spacecrafts
	for (int i = 0 ; i < ParentSector->GetSectorSpacecrafts().Num(); i++)
	{
		UFlareSimulatedSpacecraft* Spacecraft = ParentSector->GetSectorSpacecrafts()[i];
		if (Spacecraft->GetData().SpawnMode == EFlareSpawnMode::Safe && (!Spacecraft->IsReserve() ||  Parent->GetGame()->GetPC()->GetPlayerShip() == Spacecraft))
		{
			LoadSpacecraft(Spacecraft);
		}
	}

	SectorRepartitionCache = false;

	// Load unsafe location spacecrafts
	for (int i = 0; i < ParentSector->GetSectorSpacecrafts().Num(); i++)
	{
		UFlareSimulatedSpacecraft* Spacecraft = ParentSector->GetSectorSpacecrafts()[i];
		if (Spacecraft->GetData().SpawnMode != EFlareSpawnMode::Safe && (!Spacecraft->IsReserve() ||  Parent->GetGame()->GetPC()->GetPlayerShip() == Spacecraft))
		{
			LoadSpacecraft(Spacecraft);
		}
	}

	// Check docking once all spacecraft are loaded
	for (int i = 0; i < SectorSpacecrafts.Num(); i++)
	{
		SectorSpacecrafts[i]->Redock();
	}

	// Load bombs
	for (int i = 0; i < ParentSector->GetData()->BombData.Num(); i++)
	{
		LoadBomb(ParentSector->GetData()->BombData[i]);
	}
}

void UFlareSector::Save()
{
	FFlareSectorSave* SectorData  = GetSimulatedSector()->GetData();

	SectorData->BombData.Empty();
	SectorData->AsteroidData.Empty();


	for (int i = 0 ; i < SectorBombs.Num(); i++)
	{
		SectorData->BombData.Add(*SectorBombs[i]->Save());
	}

	for (int i = 0 ; i < SectorAsteroids.Num(); i++)
	{
		SectorData->AsteroidData.Add(*SectorAsteroids[i]->Save());
	}

	SectorData->LocalTime = LocalTime + GetGame()->GetPlanetarium()->GetSmoothTime();
}

void UFlareSector::DestroySector()
{
	FLOG("UFlareSector::DestroySector");

	IsDestroyingSector = true;

	// Remove spacecrafts from world
	for (int SpacecraftIndex = 0 ; SpacecraftIndex < SectorSpacecrafts.Num(); SpacecraftIndex++)
	{
		SectorSpacecrafts[SpacecraftIndex]->Destroy();
	}

	for (int BombIndex = 0 ; BombIndex < SectorBombs.Num(); BombIndex++)
	{
		SectorBombs[BombIndex]->Destroy();
	}

	for (int AsteroidIndex = 0 ; AsteroidIndex < SectorAsteroids.Num(); AsteroidIndex++)
	{
		SectorAsteroids[AsteroidIndex]->Destroy();
	}

	for (int ShellIndex = 0 ; ShellIndex < SectorShells.Num(); ShellIndex++)
	{
		SectorShells[ShellIndex]->Destroy();
	}

	SectorSpacecrafts.Empty();
	SectorShips.Empty();
	SectorStations.Empty();
	SectorBombs.Empty();
	SectorAsteroids.Empty();
	SectorShells.Empty();

	IsDestroyingSector = false;
}


/*----------------------------------------------------
	Gameplay
----------------------------------------------------*/

AFlareAsteroid* UFlareSector::LoadAsteroid(const FFlareAsteroidSave& AsteroidData)
{
    FActorSpawnParameters Params;
    Params.bNoFail = true;
	Params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;

	AFlareAsteroid* Asteroid = GetGame()->GetWorld()->SpawnActor<AFlareAsteroid>(AFlareAsteroid::StaticClass(), AsteroidData.Location, AsteroidData.Rotation, Params);
    Asteroid->Load(AsteroidData);

	// TODO Check double add
	SectorAsteroids.Add(Asteroid);
    return Asteroid;
}

AFlareSpacecraft* UFlareSector::LoadSpacecraft(UFlareSimulatedSpacecraft* ParentSpacecraft)
{
	FCHECK(ParentSpacecraft);
	FCHECK(ParentSpacecraft);
	FCHECK(ParentSpacecraft->GetDescription());
	FCHECK(ParentSpacecraft->GetDescription()->Template);

	FLOGV("UFlareSector::LoadSpacecraft : Start loading '%s' (template '%s' at %x)",
		*ParentSpacecraft->GetImmatriculation().ToString(),
		*ParentSpacecraft->GetDescription()->Template->GetName(),
		ParentSpacecraft->GetDescription()->Template);
	
	// Spawn parameters
	FActorSpawnParameters Params;
	Params.bNoFail = true;
	Params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
	auto SpacecraftClass = ParentSpacecraft->GetDescription()->Template->GeneratedClass;

	// Create and configure the ship
	AFlareSpacecraft* Spacecraft = GetGame()->GetWorld()->SpawnActor<AFlareSpacecraft>(SpacecraftClass, ParentSpacecraft->GetData().Location, ParentSpacecraft->GetData().Rotation, Params);
	if (Spacecraft && !Spacecraft->IsPendingKillPending())
	{
		Spacecraft->Load(ParentSpacecraft);
{'fraction_non_alphanumeric': 0.20455453455852976, 'fraction_numerical': 0.0024970035956851776, 'mean_word_length': 4.046875, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4213279', 'n_tokens_mistral': 3409, 'n_tokens_neox': 3267, 'n_words': 506}