void AFlareGame::Immatriculate(UFlareCompany* Company, FName TargetClass, FFlareSpacecraftSave* SpacecraftSave)
{
	FString Immatriculation;
	FString NickName;
	CurrentImmatriculationIndex++;
	FFlareSpacecraftDescription* SpacecraftDesc = SpacecraftCatalog->Get(TargetClass);
	bool IsStation = FFlareSpacecraftDescription::IsStation(SpacecraftDesc);

	// Company name
	Immatriculation += Company->GetShortName().ToString().ToUpper();

	// Class
	Immatriculation += SpacecraftDesc->ImmatriculationCode.ToString().ToUpper();

	// Name
	if (SpacecraftDesc->Size == EFlarePartSize::L && !IsStation)
	{
		NickName = PickCapitalShipName().ToString();
	}
	else
	{
		NickName = FString::Printf(TEXT("%04d"), CurrentImmatriculationIndex);
	}
	Immatriculation += FString::Printf(TEXT("-%s"), *NickName);

	// Update data
	FLOGV("AFlareGame::Immatriculate (%s) : %s", *TargetClass.ToString(), *Immatriculation);
	SpacecraftSave->Immatriculation = FName(*Immatriculation);
	SpacecraftSave->NickName = FText::FromString(NickName);
}
bool UFlareSaveGameSystem::SaveGame(const FString SaveName, UFlareSaveGame* SaveData)
{
	bool ret = false;
	SaveLock.Lock();
	FLOGV("UFlareSaveGameSystem::SaveGame SaveName=%s", *SaveName);

	UFlareSaveWriter* SaveWriter = NewObject<UFlareSaveWriter>(this, UFlareSaveWriter::StaticClass());
	TSharedRef<FJsonObject> JsonObject = SaveWriter->SaveGame(SaveData);

	// Save the json object
	FString FileContents;
	//TSharedRef< TJsonWriter<TCHAR, TCondensedJsonPrintPolicy<TCHAR>> > JsonWriter = TJsonWriterFactory<TCHAR, TCondensedJsonPrintPolicy<TCHAR>>::Create(&FileContents);
	TSharedRef< TJsonWriter<> > JsonWriter = TJsonWriterFactory<>::Create(&FileContents);

	if (FJsonSerializer::Serialize(JsonObject, JsonWriter))
	{
		JsonWriter->Close();

		ret = FFileHelper::SaveStringToFile(FileContents, *GetSaveGamePath(SaveName));
		FLOG("UFlareSaveGameSystem::SaveGame : Save done");
	}
	else
	{
		FLOGV("Fail to serialize save %s", *SaveName);
		ret = false;
	}

	SaveLock.Unlock();
	return ret;
}
UFlareSaveGame* UFlareSaveGameSystem::LoadGame(const FString SaveName)
{
	FLOGV("UFlareSaveGameSystem::LoadGame SaveName=%s", *SaveName);

	UFlareSaveGame *SaveGame = NULL;

	// Read the saveto a string
	FString SaveString;
	if(FFileHelper::LoadFileToString(SaveString, *GetSaveGamePath(SaveName)))
	{
		// Deserialize a JSON object from the string
		TSharedPtr< FJsonObject > Object;
		TSharedRef< TJsonReader<> > Reader = TJsonReaderFactory<>::Create(SaveString);
		if(FJsonSerializer::Deserialize(Reader, Object) && Object.IsValid())
		{
			UFlareSaveReaderV1* SaveReader = NewObject<UFlareSaveReaderV1>(this, UFlareSaveReaderV1::StaticClass());
			SaveGame = SaveReader->LoadGame(Object);
		}
		else
		{
			FLOGV("Fail to deserialize save '%s'", *GetSaveGamePath(SaveName));
		}
	}
	else
	{
		FLOGV("Fail to read save '%s'", *GetSaveGamePath(SaveName));
	}

	return SaveGame;
}
FFlareDockingInfo UFlareSpacecraftDockingSystem::RequestDock(AFlareSpacecraft* Ship, FVector PreferredLocation)
{
	FLOGV("UFlareSpacecraftDockingSystem::RequestDock ('%s')", *Ship->GetImmatriculation().ToString());

	int32 BestIndex = -1;
	float BestDistance = 0;

	// Looking for nearest available slot
	for (int32 i = 0; i < DockingSlots.Num(); i++)
	{
		if (!DockingSlots[i].Granted && DockingSlots[i].DockSize == Ship->GetSize())
		{
			float DockDistance = (Spacecraft->Airframe->GetComponentToWorld().TransformPosition(DockingSlots[i].LocalLocation) - PreferredLocation).Size();
			if (BestIndex < 0 || DockDistance < BestDistance)
			{
				BestDistance = DockDistance;
				BestIndex = i;
			}
		}
	}

	if (BestIndex >=0)
	{
		FLOGV("UFlareSpacecraftDockingSystem::RequestDock : found valid dock %d", BestIndex);
		DockingSlots[BestIndex].Granted = true;
		DockingSlots[BestIndex].Ship = Ship;
		return DockingSlots[BestIndex];
	}

	// Default values
	FFlareDockingInfo Info;
	Info.Granted = false;
	Info.Station = Spacecraft;
	return Info;
}
Exemple #5
0
void UFlareFleet::AddShip(UFlareSimulatedSpacecraft* Ship)
{
	if (IsTraveling())
	{
		FLOGV("Fleet Disband fail: '%s' is travelling", *GetFleetName().ToString());
		return;
	}

	if (GetCurrentSector() != Ship->GetCurrentSector())
	{
		FLOGV("Fleet Merge fail: '%s' is the sector '%s' but '%s' is the sector '%s'",
			  *GetFleetName().ToString(),
			  *GetCurrentSector()->GetSectorName().ToString(),
			  *Ship->GetImmatriculation().ToString(),
			  *Ship->GetCurrentSector()->GetSectorName().ToString());
		return;
	}

	UFlareFleet* OldFleet = Ship->GetCurrentFleet();
	if (OldFleet)
	{
		OldFleet->RemoveShip(Ship);
	}

	FleetData.ShipImmatriculations.Add(Ship->GetImmatriculation());
	FleetShips.AddUnique(Ship);
	Ship->SetCurrentFleet(this);
}
bool UFlareSaveGameSystem::SaveGame(const FString SaveName, UFlareSaveGame* SaveData)
{
	bool ret = false;
	SaveLock.Lock();
	FLOGV("UFlareSaveGameSystem::SaveGame SaveName=%s", *SaveName);

	UFlareSaveWriter* SaveWriter = NewObject<UFlareSaveWriter>(this, UFlareSaveWriter::StaticClass());
	TSharedRef<FJsonObject> JsonObject = SaveWriter->SaveGame(SaveData);

	// Save the json object
	FString FileContents;
	//TSharedRef< TJsonWriter<TCHAR, TCondensedJsonPrintPolicy<TCHAR>> > JsonWriter = TJsonWriterFactory<TCHAR, TCondensedJsonPrintPolicy<TCHAR>>::Create(&FileContents);
	TSharedRef< TJsonWriter<> > JsonWriter = TJsonWriterFactory<>::Create(&FileContents);

	if (FJsonSerializer::Serialize(JsonObject, JsonWriter))
	{
		JsonWriter->Close();

		bool Compress = true;
		if(Compress)
		{
			uint32 StrLength = FCStringAnsi::Strlen(TCHAR_TO_UTF8(*FileContents));

			uint8* CompressedDataRaw = new uint8[StrLength];
			int32 CompressedSize = StrLength;

			const bool bResult = FCompression::CompressMemory((ECompressionFlags)(COMPRESS_GZIP), CompressedDataRaw, CompressedSize, TCHAR_TO_UTF8(*FileContents), StrLength);
			if (bResult)
			{
				ret = FFileHelper::SaveArrayToFile(TArrayView<const uint8>(CompressedDataRaw, CompressedSize), *GetSaveGamePath(SaveName, true));
			}

			delete[] CompressedDataRaw;
		}
		else
		{
			ret = FFileHelper::SaveStringToFile(FileContents, *GetSaveGamePath(SaveName, false));
		}
		FLOG("UFlareSaveGameSystem::SaveGame : Save done");
	}
	else
	{
		FLOGV("Fail to serialize save %s", *SaveName);
		ret = false;
	}

	SaveLock.Unlock();

	SaveListLock.Lock();
	SaveList.Remove(SaveData);
	SaveListLock.Unlock();

	return ret;
}
FText AFlareGame::PickCapitalShipName()
{
	if (BaseImmatriculationNameList.Num() == 0)
	{
		InitCapitalShipNameDatabase();
	}
	int32 PickIndex = FMath::RandRange(0,BaseImmatriculationNameList.Num()-1);

	FText BaseName = BaseImmatriculationNameList[PickIndex];

	// Check unicity
	bool Unique;
	int32 NameIncrement = 1;
	FText CandidateName;
	do
	{
		Unique = true;
		FString Suffix;
		if (NameIncrement > 1)
		{
			FString Roman = ConvertToRoman(NameIncrement);
			Suffix = FString(" ") + Roman;
		}
		else
		{
			Suffix = FString("");
		}

		CandidateName = FText::FromString(BaseName.ToString() + Suffix);

		// Browse all existing ships the check if the name is unique
		for (int i = 0; i < GetGameWorld()->GetCompanies().Num(); i++)
		{
			UFlareCompany* Company = GetGameWorld()->GetCompanies()[i];

			// Ships
			for (int32 ShipIndex = 0 ; ShipIndex < Company->GetCompanyShips().Num(); ShipIndex++)
			{
				UFlareSimulatedSpacecraft* SpacecraftCandidate = Company->GetCompanyShips()[ShipIndex];
				if (SpacecraftCandidate && SpacecraftCandidate->GetNickName().ToString() == CandidateName.ToString())
				{
					FLOGV("Not unique %s", *CandidateName.ToString());
					Unique = false;
					break;
				}
			}
		}
		NameIncrement++;
	} while(!Unique);

	FLOGV("OK for %s", *CandidateName.ToString());

	return CandidateName;
}
void UFlareSpacecraftNavigationSystem::PushCommandRotation(const FVector& RotationTarget, const FVector& LocalShipAxis)
{
	if (RotationTarget.IsNearlyZero() || LocalShipAxis.IsNearlyZero())
	{
		return;
	}

	FFlareShipCommandData Command;
	Command.Type = EFlareCommandDataType::CDT_Rotation;
	Command.RotationTarget = RotationTarget;
	Command.LocalShipAxis = LocalShipAxis;
	FLOGV("UFlareSpacecraftNavigationSystem::PushCommandRotation RotationTarget '%s'", *RotationTarget.ToString());
	FLOGV("UFlareSpacecraftNavigationSystem::PushCommandRotation LocalShipAxis '%s'", *LocalShipAxis.ToString());
	PushCommand(Command);
}
Exemple #9
0
void UFlareFactory::BeginProduction()
{
	bool AllowDepts = !IsShipyard()
			|| (GetTargetShipCompany() != NAME_None && GetTargetShipCompany() != Parent->GetCompany()->GetIdentifier());

	if(!Parent->GetCompany()->TakeMoney(GetProductionCost(), AllowDepts, FFlareTransactionLogEntry::LogFactoryWages(this)))
	{
		return;
	}


	// Consume input resources
	for (int32 ResourceIndex = 0 ; ResourceIndex < GetCycleData().InputResources.Num() ; ResourceIndex++)
	{
		const FFlareFactoryResource* Resource = &GetCycleData().InputResources[ResourceIndex];
		FFlareCargoSave* AlreadyReservedCargo = NULL;


		// Check in reserved resources
		for (int32 ReservedResourceIndex = 0; ReservedResourceIndex < FactoryData.ResourceReserved.Num(); ReservedResourceIndex++)
		{
			if (FactoryData.ResourceReserved[ReservedResourceIndex].ResourceIdentifier ==  Resource->Resource->Data.Identifier)
			{
				AlreadyReservedCargo = &FactoryData.ResourceReserved[ReservedResourceIndex];
				break;
			}
		}

		uint32 ResourceToTake = Resource->Quantity;

		if (AlreadyReservedCargo)
		{
			ResourceToTake -= AlreadyReservedCargo->Quantity;
		}

		if (ResourceToTake <= 0)
		{
			continue;
		}

		if (Parent->GetActiveCargoBay()->TakeResources(&Resource->Resource->Data, ResourceToTake, Parent->GetCompany()) < Resource->Quantity)
		{
			FLOGV("Fail to take %d resource '%s' to %s", Resource->Quantity, *Resource->Resource->Data.Name.ToString(), *Parent->GetImmatriculation().ToString());
		}

		if (AlreadyReservedCargo)
		{
			AlreadyReservedCargo->Quantity += ResourceToTake;
		}
		else
		{
			FFlareCargoSave NewReservedResource;
			NewReservedResource.Quantity = ResourceToTake;
			NewReservedResource.ResourceIdentifier = Resource->Resource->Data.Identifier;
			FactoryData.ResourceReserved.Add(NewReservedResource);
		}
	}

	FactoryData.CostReserved = GetProductionCost();
}
UFlareCompany* AFlareGame::CreateCompany(int32 CatalogIdentifier)
{
	if (!World)
	{
		FLOG("AFlareGame::CreateCompany failed: no loaded world");
		return NULL;
	}

	UFlareCompany* Company = NULL;
	FFlareCompanySave CompanyData;

	// Generate identifier
	CurrentImmatriculationIndex++;
	FString Immatriculation = FString::Printf(TEXT("CPNY-%06d"), CurrentImmatriculationIndex);
	CompanyData.Identifier = *Immatriculation;

	// Generate arbitrary save data
	CompanyData.CatalogIdentifier = CatalogIdentifier;
	CompanyData.Money = 0;
	CompanyData.FleetImmatriculationIndex = 0;
	CompanyData.TradeRouteImmatriculationIndex = 0;
	// Create company
	Company = World->LoadCompany(CompanyData);
	FLOGV("AFlareGame::CreateCompany : Created company '%s'", *Company->GetName());

	return Company;
}
bool UFlareSpacecraftNavigationSystem::DockAt(AFlareSpacecraft* TargetStation)
{
	FLOGV("UFlareSpacecraftNavigationSystem::DockAt : '%s' docking at '%s'",
		*Spacecraft->GetParent()->GetImmatriculation().ToString(),
		*TargetStation->GetParent()->GetImmatriculation().ToString());

	FFlareDockingInfo DockingInfo = TargetStation->GetDockingSystem()->RequestDock(Spacecraft, Spacecraft->GetActorLocation());

	// Docking granted
	if (DockingInfo.Granted)
	{
		if (IsDocked())
		{
			FLOG("UFlareSpacecraftNavigationSystem::DockAt : leaving current dock");
			Undock();
		}

		FLOG("UFlareSpacecraftNavigationSystem::DockAt : access granted");
		PushCommandDock(DockingInfo);
		return true;
	}

	// Failed
	else
	{
		FLOG("UFlareSpacecraftNavigationSystem::DockAt : docking denied");
		return false;
	}
}
UFlareSimulatedSector* AFlareGame::DeactivateSector()
{
	if (!ActiveSector)
	{
		return NULL;
	}

	UFlareSimulatedSector* Sector = ActiveSector->GetSimulatedSector();
	FLOGV("AFlareGame::DeactivateSector : %s", *Sector->GetSectorName().ToString());
	World->Save();

	// Set last flown ship
	UFlareSimulatedSpacecraft* PlayerShip = NULL;
	if (GetPC()->GetPlayerShip())
	{
		PlayerShip = GetPC()->GetPlayerShip();
	}

	// Destroy the active sector
	DebrisFieldSystem->Reset();
	UnloadStreamingLevel(ActiveSector->GetSimulatedSector()->GetDescription()->LevelName);
	ActiveSector->DestroySector();
	ActiveSector = NULL;

	// Update the PC
	GetPC()->OnSectorDeactivated();
	SaveGame(GetPC());

	return Sector;
}
void UFlareSpacecraftDockingSystem::Dock(AFlareSpacecraft* Ship, int32 DockId)
{
	FLOGV("UFlareSpacecraftDockingSystem::Dock %d ('%s')", DockId, *Ship->GetParent()->GetImmatriculation().ToString());
	DockingSlots[DockId].Granted = true;
	DockingSlots[DockId].Occupied = true;
	DockingSlots[DockId].Ship = Ship;
}
bool AFlareGame::LoadGame(AFlarePlayerController* PC)
{
	FLOGV("AFlareGame::LoadGame : loading from slot %d", CurrentSaveIndex);
	PlayerController = PC;
	Clean();
	PC->Clean();

	UFlareSaveGame* Save = ReadSaveSlot(CurrentSaveIndex);

	// Load from save
	if (PC && Save)
	{
		PC->SetCompanyDescription(Save->PlayerCompanyDescription);

        // Create the new world
        World = NewObject<UFlareWorld>(this, UFlareWorld::StaticClass());

		FLOGV("AFlareGame::LoadGame date=%lld", Save->WorldData.Date);

        World->Load(Save->WorldData);
		CurrentImmatriculationIndex = Save->CurrentImmatriculationIndex;
		
        // TODO check if load is ok for ship event before the PC load

		// Load the player
		PC->Load(Save->PlayerData);
		PC->GetCompany()->SetupEmblem();

		World->CheckIntegrity();

		// Init the quest manager
		QuestManager = NewObject<UFlareQuestManager>(this, UFlareQuestManager::StaticClass());
		QuestManager->Load(Save->PlayerData.QuestData);

		LoadedOrCreated = true;
		PC->OnLoadComplete();

		return true;
	}

	// No file existing
	else
	{
		FLOGV("AFlareGame::LoadWorld : could lot load slot %d", CurrentSaveIndex);
		return false;
	}
}
bool UFlareSpacecraftNavigationSystem::Undock()
{
	// Try undocking
	if (IsDocked())
	{
		FLOGV("UFlareSpacecraftNavigationSystem::Undock : '%s' undocking from '%s'",
			*Spacecraft->GetParent()->GetImmatriculation().ToString(),
			*Data->DockedTo.ToString());

		// Detach from station
		if(DockConstraint)
		{
			DockConstraint->BreakConstraint();
			DockConstraint->DestroyComponent();
			DockConstraint = NULL;
		}

		AFlareSpacecraft* DockStation = GetDockStation();
		DockStation->GetDockingSystem()->ReleaseDock(Spacecraft, Data->DockedAt);

		// Update data
		SetStatus(EFlareShipStatus::SS_AutoPilot);
		Data->DockedTo = NAME_None;
		Data->DockedAt = -1;

		// Update Angular acceleration rate : when it's docked the mass is the ship mass + the station mass
		Spacecraft->SetRCSDescription(Spacecraft->GetRCSDescription());
		Spacecraft->OnUndocked(DockStation);

		// Leave
		PushCommandLocation(Spacecraft->GetRootComponent()->GetComponentTransform().TransformPositionNoScale(5000 * FVector(-1, 0, 0)));
		FLOG("UFlareSpacecraftNavigationSystem::Undock : successful");

		// Hack for bug #195: for ue4 to reweld all.
		Spacecraft->Airframe->SetSimulatePhysics(false);
		Spacecraft->Airframe->SetSimulatePhysics(true);

		return true;
	}

	// Failed
	else
	{
		FLOGV("UFlareSpacecraftNavigationSystem::Undock : '%s' is not docked", *Spacecraft->GetParent()->GetImmatriculation().ToString());
		return false;
	}
}
void UFlareSaveWriter::SaveFloat(TSharedPtr< FJsonObject > Object, FString Key, float Data)
{
	if(FMath::IsNaN(Data))
	{
		FLOGV("WARNING: Fix NaN in code for field '%s' : %f", *Key, Data);
		Object->SetNumberField(Key, 0);
	}
	else if(!FMath::IsFinite(Data))
	{
		FLOGV("WARNING: Fix Inf in code for field '%s' : %f", *Key, Data);
		Object->SetNumberField(Key, 0);
	}
	else
	{
		Object->SetNumberField(Key, Data);
	}
}
Exemple #17
0
void UFlareCargoBay::SetSlotRestriction(int32 SlotIndex, EFlareResourceRestriction::Type RestrictionType)
{
	if(SlotIndex >= CargoBay.Num())
	{
		FLOGV("Invalid index %d for set slot restriction (cargo bay size: %d)", SlotIndex, CargoBay.Num());
	}
	CargoBay[SlotIndex].Restriction = RestrictionType;
}
void UFlareInternalComponent::Initialize(FFlareSpacecraftComponentSave* Data, UFlareCompany* Company, AFlareSpacecraftPawn* OwnerShip, bool IsInMenu)
{
	Super::Initialize(Data, Company, OwnerShip, IsInMenu);
	if (!ComponentDescription)
	{
		FLOGV("!!! Internal component %s is not correctly mapped :", *(this->GetReadableName()));
	}
}
void AFlareGame::Scrap(FName ShipImmatriculation, FName TargetStationImmatriculation)
{
	DeactivateSector();

	UFlareSimulatedSpacecraft* ShipToScrap = World->FindSpacecraft(ShipImmatriculation);
	UFlareSimulatedSpacecraft* ScrapingStation = World->FindSpacecraft(TargetStationImmatriculation);

	if(!ShipToScrap || !ScrapingStation)
	{
		FLOG("Scrap failed: ship to scrap or station not found");
		return;
	}

	if(ShipToScrap->GetCurrentSector() != ScrapingStation->GetCurrentSector())
	{
		FLOG("Scrap failed: ship and station not in the same sector");
		return;
	}
	UFlareSimulatedSector* CurrentSector = ShipToScrap->GetCurrentSector();

	int64 ScrapRevenue = 0;

	for (int ResourceIndex = 0; ResourceIndex < ShipToScrap->GetDescription()->CycleCost.InputResources.Num() ; ResourceIndex++)
	{
		FFlareFactoryResource* Resource = &ShipToScrap->GetDescription()->CycleCost.InputResources[ResourceIndex];


		ScrapRevenue += Resource->Quantity * CurrentSector->GetResourcePrice(&Resource->Resource->Data, EFlareResourcePriceContext::Default);
		int ResourceToGive = Resource->Quantity;

		ResourceToGive -= ScrapingStation->GetCargoBay()->GiveResources(&Resource->Resource->Data, Resource->Quantity);
		CurrentSector->GiveResources(ScrapingStation->GetCompany(), &Resource->Resource->Data, ResourceToGive, true);
	}


	ScrapRevenue = FMath::Min(ScrapRevenue, ScrapingStation->GetCompany()->GetMoney());

	FLOGV("Scrap success for %d", ScrapRevenue);

	if (ScrapingStation->GetCompany() != ShipToScrap->GetCompany())
	{
		ScrapingStation->GetCompany()->TakeMoney(ScrapRevenue);
		ShipToScrap->GetCompany()->GiveMoney(ScrapRevenue);
		GetPC()->Notify(LOCTEXT("ShipSellScrap", "Ship scrap complete"),
			FText::Format(LOCTEXT("ShipSellScrapFormat", "Your ship {0} has been scrapped for {1} credits!"), FText::FromString(ShipToScrap->GetImmatriculation().ToString()), FText::AsNumber(UFlareGameTools::DisplayMoney(ScrapRevenue))),
			FName("ship-own-scraped"),
			EFlareNotification::NT_Economy);
	}
	else
	{
		GetPC()->Notify(LOCTEXT("ShipOwnScrap", "Ship scrap complete"),
			FText::Format(LOCTEXT("ShipOwnScrapFormat", "Your ship {0} has been scrapped !"), FText::FromString(ShipToScrap->GetImmatriculation().ToString())),
			FName("ship-own-scraped"),
			EFlareNotification::NT_Economy);
	}

	ShipToScrap->GetCompany()->DestroySpacecraft(ShipToScrap);
}
void UFlareSpacecraftNavigationSystem::PushCommand(const FFlareShipCommandData& Command)
{
	SetStatus(EFlareShipStatus::SS_AutoPilot);
	CommandData.Enqueue(Command);

	FLOGV("UFlareSpacecraftNavigationSystem::PushCommand : '%s' has new command '%s'",
		*Spacecraft->GetParent()->GetImmatriculation().ToString(),
		*EFlareCommandDataType::ToString(Command.Type));
}
void UFlareSpacecraftNavigationSystem::ConfirmDock(AFlareSpacecraft* DockStation, int32 DockId)
{
	FLOGV("UFlareSpacecraftNavigationSystem::ConfirmDock : '%s' is now docked", *Spacecraft->GetParent()->GetImmatriculation().ToString());
	ClearCurrentCommand();

	// Set as docked
	DockStation->GetDockingSystem()->Dock(Spacecraft, DockId);
	SetStatus(EFlareShipStatus::SS_Docked);
	Data->DockedTo = DockStation->GetImmatriculation();
	Data->DockedAt = DockId;

	if(DockConstraint)
	{
		DockConstraint->BreakConstraint();
		DockConstraint->DestroyComponent();
		DockConstraint = NULL;
	}

	// Attach to station
	FConstraintInstance ConstraintInstance;
	ConstraintInstance.bDisableCollision = true;
	ConstraintInstance.AngularSwing1Motion = ACM_Locked;
	ConstraintInstance.AngularSwing2Motion = ACM_Locked;
	ConstraintInstance.AngularTwistMotion = ACM_Locked;
	ConstraintInstance.LinearXMotion = LCM_Locked;
	ConstraintInstance.LinearYMotion = LCM_Locked;
	ConstraintInstance.LinearZMotion = LCM_Locked;
	ConstraintInstance.AngularRotationOffset = FRotator::ZeroRotator;
	ConstraintInstance.bSwingLimitSoft = 0;
	ConstraintInstance.bTwistLimitSoft = 0;
	ConstraintInstance.bLinearLimitSoft = 0;
	ConstraintInstance.bLinearBreakable = 0;
	ConstraintInstance.bAngularBreakable = 0;


	DockConstraint = NewObject<UPhysicsConstraintComponent>(Spacecraft->Airframe);
	DockConstraint->ConstraintInstance = ConstraintInstance;
	DockConstraint->SetWorldLocation(Spacecraft->GetActorLocation());
	DockConstraint->AttachToComponent(Spacecraft->GetRootComponent(), FAttachmentTransformRules(EAttachmentRule::KeepWorld, false), NAME_None);

	DockConstraint->SetConstrainedComponents(Spacecraft->Airframe, NAME_None, DockStation->Airframe,NAME_None);

	// Cut engines
	TArray<UActorComponent*> Engines = Spacecraft->GetComponentsByClass(UFlareEngine::StaticClass());
	for (int32 EngineIndex = 0; EngineIndex < Engines.Num(); EngineIndex++)
	{
		UFlareEngine* Engine = Cast<UFlareEngine>(Engines[EngineIndex]);
		Engine->SetAlpha(0.0f);
	}


	Spacecraft->OnDocked(DockStation);
}
Exemple #22
0
uint32 UFlareFactory::GetOutputLimit(FFlareResourceDescription* Resource)
{
	for (int32 CargoLimitIndex = 0 ; CargoLimitIndex < FactoryData.OutputCargoLimit.Num() ; CargoLimitIndex++)
	{
		if (FactoryData.OutputCargoLimit[CargoLimitIndex].ResourceIdentifier == Resource->Identifier)
		{
			return FactoryData.OutputCargoLimit[CargoLimitIndex].Quantity;
		}
	}
	FLOGV("No output limit for %s", *Resource->Identifier.ToString());
	return 0;
}
Exemple #23
0
void UFlareFactory::TryBeginProduction()
{
	if (GetMarginRatio() < 0.f)
	{
		FLOGV("WARNING: Margin ratio for %s is : %f", *FactoryDescription->Name.ToString(), GetMarginRatio())
	}

	if (IsNeedProduction() && !HasCostReserved() && HasInputResources() && HasInputMoney())
	{
		BeginProduction();
	}

}
void AFlarePlanetarium::SetupCelestialBodies()
{
	// Sort by incresing distance
	BodyPositions.Sort(&BodyDistanceComparator);
	double BaseDistance = GetGame()->GetActiveSector()->GetSectorLimits() * 2; // Min distance, 15km
	double BaseIncrement = BaseDistance;
#ifdef PLANETARIUM_DEBUG
	DrawDebugSphere(GetWorld(), FVector::ZeroVector, (BaseDistance /2) /1000 , 32, FColor::Red, false);
#endif
	for(int32 BodyIndex = 0; BodyIndex < BodyPositions.Num(); BodyIndex++)
	{
		CelestialBodyPosition* BodyPosition = &BodyPositions[BodyIndex];

		// The surface must be at the base distance.
		// So the display is the base distance + the display radius
		// But the display radius depend on the base distance

		double EffectiveRadius = BodyPosition->Radius;

		// If outside the ring take ring radius instead of planet
		if(BodyIndex == 0 && BodyPosition->Distance > BodyPosition->Radius + BodyPosition->Body->RingsOuterAltitude)
		{
			EffectiveRadius += BodyPosition->Body->RingsOuterAltitude;
		}

		double AngularRadius = FPreciseMath::Atan(BodyPosition->Radius / BodyPosition->Distance);
		double AngularRadiusWithRings = FPreciseMath::Atan(EffectiveRadius / BodyPosition->Distance);

		double DisplayDistance = (BaseDistance) / (1- FPreciseMath::Tan(AngularRadiusWithRings));
		double DisplayRadius = FPreciseMath::Tan(AngularRadius) * DisplayDistance;

		SetupCelestialBody(BodyPosition, DisplayDistance, DisplayRadius);
#ifdef PLANETARIUM_DEBUG
		FLOGV("SetupCelestialBodies %s BodyPosition->Radius = %f", *BodyPosition->Body->Identifier.ToString(), BodyPosition->Radius);
		FLOGV("SetupCelestialBodies %s BodyPosition->Distance = %f", *BodyPosition->Body->Identifier.ToString(), BodyPosition->Distance);
		FLOGV("SetupCelestialBodies %s BaseDistance = %f", *BodyPosition->Body->Identifier.ToString(), BaseDistance);
		FLOGV("SetupCelestialBodies %s AngularRadius = %f", *BodyPosition->Body->Identifier.ToString(), AngularRadius);
		FLOGV("SetupCelestialBodies %s DisplayDistance = %f", *BodyPosition->Body->Identifier.ToString(), DisplayDistance);
		FLOGV("SetupCelestialBodies %s DisplayRadius = %f", *BodyPosition->Body->Identifier.ToString(), DisplayRadius);
		FLOGV("SetupCelestialBodies %s EffectiveRadius = %f", *BodyPosition->Body->Identifier.ToString(), EffectiveRadius);
#endif

		// Update BaseDistance for future bodies. Take margin for Nema rings
		if(BodyIndex == 0 && BodyPosition->Body->RingsOuterAltitude == 0)
		{
			BaseDistance = DisplayDistance + DisplayRadius / 2;
		}
		else
		{
			BaseDistance = DisplayDistance + DisplayRadius;
		}
	}
}
void UFlareSpacecraftNavigationSystem::ClearCurrentCommand()
{
	FFlareShipCommandData Command;
	CommandData.Dequeue(Command);

	FLOGV("UFlareSpacecraftNavigationSystem::ClearCurrentCommand : '%s' has cleared command '%s'",
		*Spacecraft->GetParent()->GetImmatriculation().ToString(),
		*EFlareCommandDataType::ToString(Command.Type));

	if (!CommandData.Peek(Command))
	{
		SetStatus(EFlareShipStatus::SS_Manual);
	}
}
bool AFlareGame::SaveGame(AFlarePlayerController* PC)
{
	if (!IsLoadedOrCreated())
	{
		FLOG("AFlareGame::SaveGame : no game loaded, aborting");
		return false;
	}

	FLOGV("AFlareGame::SaveGame : saving to slot %d", CurrentSaveIndex);
	UFlareSaveGame* Save = Cast<UFlareSaveGame>(UGameplayStatics::CreateSaveGameObject(UFlareSaveGame::StaticClass()));
	
	// Save process
	if (PC && Save)
	{
		// Save the player
		PC->Save(Save->PlayerData, Save->PlayerCompanyDescription);
		Save->WorldData = *World->Save();
		Save->CurrentImmatriculationIndex = CurrentImmatriculationIndex;
		Save->PlayerData.QuestData = *QuestManager->Save();

		FLOGV("AFlareGame::SaveGame date=%lld", Save->WorldData.Date);
		// Save
		FString SaveName = "SaveSlot" + FString::FromInt(CurrentSaveIndex);

		// Save prototype
		SaveGameSystem->SaveGame(SaveName, Save);

		return true;
	}

	// No PC
	else
	{
		FLOG("AFlareGame::SaveGame failed");
		return false;
	}
}
TArray<AFlareSpacecraft*> UFlareSpacecraftDockingSystem::GetDockedShips()
{
	TArray<AFlareSpacecraft*> Result;

	for (int32 i = 0; i < DockingSlots.Num(); i++)
	{
		if (DockingSlots[i].Granted && DockingSlots[i].Occupied)
		{
			FLOGV("UFlareSpacecraftDockingSystem::GetDockedShips : found valid dock %d", i);
			Result.AddUnique(DockingSlots[i].Ship);
		}
	}

	return Result;
}
void SFlareNewGameMenu::OnLaunch()
{
	AFlarePlayerController* PC = MenuManager->GetPC();
	if (PC && Game && !Game->IsLoadedOrCreated())
	{
		// Get data
		FText CompanyNameData = FText::FromString(CompanyName->GetText().ToString().Left(60)); // FString needed here
		int32 ScenarioIndex = ScenarioList.Find(ScenarioSelector->GetSelectedItem());
		FLOGV("SFlareNewGameMenu::OnLaunch : '%s', scenario %d", *CompanyNameData.ToString(), ScenarioIndex);

		// Launch
		Game->CreateGame(PC, CompanyNameData, ScenarioIndex, TutorialButton->IsActive());
		MenuManager->OpenMenu(EFlareMenu::MENU_Story);
	}
}
void AFlareGame::UnloadStreamingLevel(FName SectorLevel)
{
	if (SectorLevel != NAME_None)
	{
		FLOGV("AFlareGame::UnloadStreamingLevel : Unloading streaming level '%s'", *SectorLevel.ToString());

		FLatentActionInfo Info;
		Info.CallbackTarget = this;
		Info.ExecutionFunction = "OnLevelUnloaded";
		Info.UUID = CurrentStreamingLevelIndex;
		Info.Linkage = 0;

		UGameplayStatics::UnloadStreamLevel(this, SectorLevel, Info);
		CurrentStreamingLevelIndex++;
	}
}
Exemple #30
0
void UFlareFleet::Merge(UFlareFleet* Fleet)
{
	FText Unused;
	if (!CanMerge(Fleet, Unused))
	{
		FLOGV("Fleet Merge fail: '%s' is not mergeable", *Fleet->GetFleetName().ToString());
		return;
	}

	TArray<UFlareSimulatedSpacecraft*> Ships = Fleet->GetShips();
	Fleet->Disband();
	for (int ShipIndex = 0; ShipIndex < Ships.Num(); ShipIndex++)
	{
		AddShip(Ships[ShipIndex]);
	}
}