Implemented Fair NPC Station Randomizer

This commit is contained in:
Rafal Swierczek 2024-05-12 17:52:54 +01:00
parent 956da86677
commit 5bec94540b
3 changed files with 22 additions and 5 deletions

View File

@ -38,9 +38,26 @@ void ANPC_Manager::BeginPlay()
if (NPC_StationClasses.IsEmpty()) return; if (NPC_StationClasses.IsEmpty()) return;
FRotator DefaultRot = FRotator(0, 0, 0); FRotator DefaultRot = FRotator(0, 0, 0);
int StationSlots = (StationPoints.Num() / NPC_StationClasses.Num()) + 1;
TArray<int> NumOfUniqueStations;
for (auto UniqueStation : NPC_StationClasses)
{
NumOfUniqueStations.Add(StationSlots);
}
for (FVector StationPoint : StationPoints) for (FVector StationPoint : StationPoints)
{ {
int RandInt = FMath::RandRange(0, NPC_StationClasses.Num() - 1); bool FoundRandomFairInt = false;
int RandInt = 0;
while (!FoundRandomFairInt)
{
RandInt = FMath::RandRange(0, NPC_StationClasses.Num() - 1);
if (NumOfUniqueStations[RandInt] > 0)
{
NumOfUniqueStations[RandInt]--;
FoundRandomFairInt = true;
}
}
FVector StationPointWorldLocation = StationPoint + GetActorLocation(); FVector StationPointWorldLocation = StationPoint + GetActorLocation();
ANPC_Station* NPC_Station = GetWorld()->SpawnActor<ANPC_Station>(NPC_StationClasses[RandInt], StationPointWorldLocation, DefaultRot); ANPC_Station* NPC_Station = GetWorld()->SpawnActor<ANPC_Station>(NPC_StationClasses[RandInt], StationPointWorldLocation, DefaultRot);
if (IsValid(NPC_Station)) NPC_Stations.Add(NPC_Station); if (IsValid(NPC_Station)) NPC_Stations.Add(NPC_Station);