AzureAbyss/Source/the_twilight_abyss/Quest/Quest.cpp

79 lines
2.3 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Quest.h"
#include "the_twilight_abyss/BaseItems/InventoryComponent.h"
bool UQuest::CheckConditions(UWorldState* WorldState) const
{
if (!IsValid(Goals)) return true;
if (WorldStateMatch(WorldState, Goals)) return true;
return false;
}
bool UQuest::CheckPreConditions(UWorldState* WorldState) const
{
if (!IsValid(PreConditions)) return true;
if (WorldStateMatch(WorldState, PreConditions)) return true;
return false;
}
void UQuest::ApplyRewards(UInventoryComponent* Inventory)
{
if (Rewards.IsEmpty()) return;
for (UBaseItem* Item : Rewards)
{
Inventory->AddItem(Item);
}
}
bool UQuest::WorldStateMatch(UWorldState* A, UWorldState* B)
{
if (A->Items.IsEmpty() && !B->Items.IsEmpty()) return false;
for (UBaseItem* Item : B->Items)
{
if (A->Items.IndexOfByPredicate([Item](const UBaseItem* AItem) { return AItem->ItemID == Item->ItemID; }) == -1)
{
UE_LOG(LogTemp, Warning, TEXT("WorldState Predicate Failed"));
return false;
}
if (!A->Items.Contains(Item) && Item->StackCount > A->Items[A->Items.IndexOfByPredicate([Item](const UBaseItem* AItem) { return AItem->ItemID == Item->ItemID; })]->StackCount)
{
return false;
}
}
if (A->QuestFlags.IsEmpty() && !B->QuestFlags.IsEmpty()) return false;
for (TTuple<FString, bool> Flag : B->QuestFlags)
{
if (!A->QuestFlags.Contains(Flag.Key) || A->QuestFlags[Flag.Key] != Flag.Value)
{
return false;
}
}
return true;
}
bool UQuest::FlagTrueMatch(const UWorldState* WorldState, const FString& FlagName)
{
if (!WorldState->QuestFlags.Contains(FlagName)) return false;
return true;
}
FString UQuest::GetItemCountAndGoalAmount(const UWorldState* WorldState, UBaseItem* Item) const
{
FString Result;
if (WorldState->Items.ContainsByPredicate([Item](const UBaseItem* AItem) { return AItem->ItemID == Item->ItemID; }))
{
Result += FString::FromInt(WorldState->Items[WorldState->Items.IndexOfByPredicate([Item](const UBaseItem* AItem) { return AItem->ItemID == Item->ItemID; })]->StackCount);
Result += "/";
Result += FString::FromInt(Goals->Items[Goals->Items.Find(Item)]->StackCount);
return Result;
}
Result += "0/";
Result += FString::FromInt(Goals->Items[Goals->Items.Find(Item)]->StackCount);
return Result;
}