Files
Ivazovsky/Source/GraffModule/Private/cppFuncLibrary.cpp
2024-05-11 17:04:56 +05:00

444 lines
12 KiB
C++

// Fill outLvl your copyright notice in the Description page of Project Settings.
#include "cppFuncLibrary.h"
#include "cppGI.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetStringLibrary.h"
#include "Kismet/KismetTextLibrary.h"
#include "Kismet/KismetmathLibrary.h"
bool operator==(const Fcppcoords c1, const Fcppcoords c2) {
return c1.flat == c2.flat &&
c1.floor == c2.floor &&
c1.house == c2.house &&
c1.section == c2.section &&
c1.type == c2.type &&
c1.zone == c2.zone;
}
//
//bool operator==(const FcppLevelStruct& c1, const FcppLevelStruct& c2) {
// return c1.anotherLvl == c2.anotherLvl &&
// c1.coords == c2.coords &&
// c1.dontUnload == c2.dontUnload &&
// c1.path == c2.path &&
// c1.uniqName == c2.uniqName &&
// c1.Z == c2.Z;
//}
TArray<FString> UcppFuncLibrary::cppUniversalParser(FString string, TArray<FString> keys, FString delimiter, bool caseSensitive)
{
TArray<FString> arr;
TArray<FString> out;
out.SetNum(keys.Num());
string.ParseIntoArray(arr, *delimiter, false);
for (const auto& str : arr) {
int n = 0;
for (const auto& str1 : keys) {
if (str1.IsEmpty()) continue;
FString left_;
FString right_;
if (str.Split(str1, &left_, &right_, caseSensitive ? ESearchCase::CaseSensitive : ESearchCase::IgnoreCase)) {
if (out[n].IsEmpty()) out[n] = right_;
}
n++;
}
}
return out;
}
FString UcppFuncLibrary::cppIntToStrPad(int32 int_, int32 minDigits)
{
FString temp = FString::FromInt(int_);
for (int32 i = temp.Len(); i < minDigits; i++) {
temp = L"0" + temp;
}
return temp;
}
TSoftObjectPtr<UTexture2D> UcppFuncLibrary::castSoftTex2D(TSoftObjectPtr<UObject> ptr)
{
return TSoftObjectPtr<UTexture2D>(ptr.ToSoftObjectPath());//somewhy cant construct from another <T> and cant cast
}
double UcppFuncLibrary::NegateF(double in)
{
return in * -1;
}
int32 UcppFuncLibrary::NegateI(int32 in)
{
return in * -1;
}
double UcppFuncLibrary::OneMinus(double in)
{
return 1 - in;
}
double UcppFuncLibrary::AddRefF(UPARAM(ref) double& A, double B)
{
A += B;
return A;
}
int32 UcppFuncLibrary::AddRefI(UPARAM(ref) int32& A, int32 B)
{
A += B;
return A;
}
double UcppFuncLibrary::SubRefF(UPARAM(ref) double& A, double B)
{
A -= B;
return A;
}
int32 UcppFuncLibrary::SubRefI(UPARAM(ref) int32& A, int32 B)
{
A -= B;
return A;
}
double UcppFuncLibrary::saturate(double in)
{
return in < 0 ? 0 : (in > 1 ? 1 : in);
}
int32 UcppFuncLibrary::clamp0(int32 in)
{
return (in < 0) ? 0 : in;
}
void UcppFuncLibrary::cppssw(int32 int_, FString trueInts, bool& true_, bool& false_)
{
FJsonSerializableArray arr;
trueInts.ParseIntoArray(arr, L",");
for (auto& obj : arr) {
FString lft;
FString* lftp = &lft;
FString rht;
FString* rhtp = &rht;
if (obj.Split(L"-", &lft, &rht)) {
if ((int_ >= UKismetStringLibrary::Conv_StringToInt(lft)) && (int_ <= UKismetStringLibrary::Conv_StringToInt(rht))) {
true_ = true;
false_ = false;
return;
}
}
else {
if (int_ == UKismetStringLibrary::Conv_StringToInt(obj)) {
true_ = true;
false_ = false;
return;
}
}
}
true_ = false;
false_ = true;
return;
}
FString UcppFuncLibrary::stringAdd(UPARAM(ref)FString& str, FString add)
{
return str.Append(add);
}
void UcppFuncLibrary::parseOptions(FString SourceString, FString key, FString Delimiter, FString& value, bool& found)
{
FJsonSerializableArray arr;
SourceString.ParseIntoArray(arr, *Delimiter);
for (auto& ar : arr) {
FString lft;
FString rht;
if (ar.Split(L"=", &lft, &rht)) {
if (lft == key) {
value = rht;
found = true;
return;
}
}
}
value = FString();
found = false;
return;
}
int32 UcppFuncLibrary::customStringToInt(FString string)
{
return (string.IsEmpty() || !string.IsNumeric()) ? -1 : UKismetStringLibrary::Conv_StringToInt(string);
}
void UcppFuncLibrary::multiContains(FString source, UPARAM(ref)TArray<FString>& substrings, bool bAnd, bool bUseCase, bool& bTrue, bool& bFalse)
{
TArray<bool> temp;
for (auto& st : substrings) {
bool temp1 = source.Contains(st, bUseCase ? ESearchCase::CaseSensitive : ESearchCase::IgnoreCase);
temp.Add(temp1);
if (temp1) {
if (!bAnd) {
bTrue = true;
bFalse = false;
return;
}
}
}
bTrue = !temp.Contains(false);
bFalse = !bTrue;
return;
}
void UcppFuncLibrary::containStringSwitch(FString string, UPARAM(ref)TArray<FString>& keys, bool bUseCase, int32& bInt, TArray<bool>& bools)
{
bools.Empty();
bInt = -1;
bool lock = false;
for (auto& key : keys) {
bool temp2 = string.Contains(key, bUseCase ? ESearchCase::CaseSensitive : ESearchCase::IgnoreCase);
bools.Add(temp2);
if (temp2 && !lock) {
lock = true;
bInt = bools.Num() - 1;
}
}
}
FString UcppFuncLibrary::multiRemove(FString source, UPARAM(ref)TArray<FString>& substrings, bool bUseCase)
{
for (auto& ss : substrings) source.ReplaceInline(*ss, L"", bUseCase ? ESearchCase::CaseSensitive : ESearchCase::IgnoreCase);
return source;
}
FString UcppFuncLibrary::quoteString(FString string)
{
return FString("\"").Append(string).Append("\"");
}
void UcppFuncLibrary::getRange(double value, double minMult, double maxMult, double& bMin, double& bValue, double& bMax)
{
bMin = value * minMult;
bMax = value * maxMult;
bValue = value;
}
void UcppFuncLibrary::updateFilterList(UObject* WorldContextObject, int floorMin, int floorMax, float sqMin, float sqMax, int priceMin, int priceMax, TArray<bool> houses, TArray<bool> sections, TArray<bool> rooms, TArray<bool> Tags, int sortType, bool onlyAvailable, TArray<FcppflatStruct>& filtered, int& count, TArray<FIntPoint> houseMap)
{
for (auto& hm : houseMap) {
}
auto gi = StaticCastPtr<UcppGI, UGameInstance>(UGameplayStatics::GetGameInstance(WorldContextObject));
if (!gi) return;
TArray<int> tempfloor, tempprice;
TArray<float> tempsq;
for (auto& flat : gi->flatArray) {
if (inRange(flat.Floor, floorMin, floorMax)
&& inRange(flat.Price, priceMin, priceMax)
&& inRange(flat.Square, sqMin, sqMax)
//&& boolGet(houses, flat.House)
//&& boolGet(sections, flat.Section)
&& [](TArray<FIntPoint>& houseMap_, TArray<bool>& houses_, TArray<bool>& sections_, FcppflatStruct& flat_) {
if (houseMap_.IsEmpty()) return boolGet(houses_, flat_.House) && boolGet(sections_, flat_.Section); //if no map use classic
if (!houses_.Contains(true)) return true; //if no select then pass
for (int n = 0; n < houses_.Num(); n++) {
if (houseMap_.IsValidIndex(n)) {
if (houseMap_[n].X == flat_.House && houseMap_[n].Y == flat_.Section) return houses_[n];
}
}
return false;
}(houseMap, houses, sections, flat)
&& boolGet(rooms, flat.Rooms)
&& [](TArray<bool> tags, TArray<bool> ftags) {
int n = 0;
for (auto& t : tags) {
if (ftags.IsValidIndex(n))
{
if (t && !ftags[n]) return false;
}
n++;
}
return true;
}(Tags, flat.tags)
&& (onlyAvailable ? flat.available : true)) {
filtered.Add(flat);
tempfloor.Add(flat.Floor);
tempprice.Add(flat.Price);
tempsq.Add(flat.Square);
}
}
count = filtered.Num();
if (tempfloor.Num() > 1) {
switch (sortType) {
case 0:
case 1:
for (int i = 0; i <= (tempprice.Num() - 1) / 2; i++) {
int minid, maxid;
minmaxInRange(tempprice, i, tempprice.Num() - 1 - i, minid, maxid);
tempprice.Swap(sortType == 0 ? minid : maxid, i);
filtered.Swap(sortType == 0 ? minid : maxid, i);
minmaxInRange(tempprice, i, tempprice.Num() - 1 - i, minid, maxid);
tempprice.Swap(sortType == 1 ? minid : maxid, tempprice.Num() - 1 - i);
filtered.Swap(sortType == 1 ? minid : maxid, tempprice.Num() - 1 - i);
}
break;
case 2:
case 3:
for (int i = 0; i <= (tempsq.Num() - 1) / 2; i++) {
int minid, maxid;
minmaxInRange(tempsq, i, tempsq.Num() - 1 - i, minid, maxid);
tempsq.Swap(sortType == 2 ? minid : maxid, i);
filtered.Swap(sortType == 2 ? minid : maxid, i);
minmaxInRange(tempsq, i, tempsq.Num() - 1 - i, minid, maxid);
tempsq.Swap(sortType == 3 ? minid : maxid, tempsq.Num() - 1 - i);
filtered.Swap(sortType == 3 ? minid : maxid, tempsq.Num() - 1 - i);
}
break;
case 4:
case 5:
for (int i = 0; i <= (tempfloor.Num() - 1) / 2; i++) {
int minid, maxid;
minmaxInRange(tempfloor, i, tempfloor.Num() - 1 - i, minid, maxid);
tempfloor.Swap(sortType == 4 ? minid : maxid, i);
filtered.Swap(sortType == 4 ? minid : maxid, i);
minmaxInRange(tempfloor, i, tempfloor.Num() - 1 - i, minid, maxid);
tempfloor.Swap(sortType == 5 ? minid : maxid, tempfloor.Num() - 1 - i);
filtered.Swap(sortType == 5 ? minid : maxid, tempfloor.Num() - 1 - i);
}
break;
default:
break;
}
}
}
void UcppFuncLibrary::groupFiltered(UPARAM(ref)TArray<FcppflatStruct>& filtered, UPARAM(ref)TArray<FcppflatStruct>& filtered_grouped)
{
TArray<Fcppcoords> filteredCoords;
for (auto& f : filtered) {
filteredCoords.Add(Fcppcoords(f.FlatId, f.flatType, f.Zone, f.House, f.Section));
}
TArray<Fcppcoords> filtered_grCoords;
//for (auto& f : filtered_grouped) {
// Fcppcoords ff;
// ff.flat = f.FlatId;
// ff.house = f.House;
// ff.section = f.Section;
// ff.type = f.flatType;
// ff.zone = f.Zone;
// //skip floor
// filtered_grCoords.Add(ff);
//}
filtered_grouped.Empty();
int nn = 0;
for (auto& obj : filtered) {
if (filtered_grCoords.Contains(filteredCoords[nn])) {
auto fc = filtered_grCoords.Find(filteredCoords[nn]);
FJsonSerializableArray opts;
filtered_grouped[fc].comment.ParseIntoArray(opts, L";");
FString l;
FString r;
opts[0].Split(L"=", &l, &r);
//auto cn = FCString::Atoi(*r); //current N
auto cn = UKismetStringLibrary::Conv_StringToInt(r);
opts[1].Split(L"=", &l, &r);
//auto csqf=FCString::Atof(*r);// current sqfrom
auto csqf = UKismetStringLibrary::Conv_StringToFloat(r);
opts[2].Split(L"=", &l, &r);
//auto csqt=FCString::Atof(*r);// current sqto
auto csqt = UKismetStringLibrary::Conv_StringToFloat(r);
cn++;
csqt = obj.Square > csqt ? obj.Square : csqt;
csqf = obj.Square < csqf ? obj.Square : csqf;
if (filtered_grouped[fc].Price > obj.Price) filtered_grouped[fc].Price = obj.Price;
filtered_grouped[fc].comment =
L"n="
+ FString::FromInt(cn)
+ L";sqfrom="
+ UKismetTextLibrary::Conv_FloatToText(csqf, ERoundingMode::HalfToEven, false, false, 1, 5, 2, 2).ToString()
+ L";sqto="
+ UKismetTextLibrary::Conv_FloatToText(csqt, ERoundingMode::HalfToEven, false, false, 1, 5, 2, 2).ToString();
filtered_grouped[fc].comment.ReplaceCharInline(*L",", *L".");
obj.comment = filtered_grouped[fc].comment;
//here is place for your commercial
}
else {
obj.comment = L"n=1;sqfrom="
+ UKismetTextLibrary::Conv_FloatToText(obj.Square, ERoundingMode::HalfToEven, false, false, 1, 5, 2, 2).ToString()
+ L";sqto="
+ UKismetTextLibrary::Conv_FloatToText(obj.Square, ERoundingMode::HalfToEven, false, false, 1, 5, 2, 2).ToString();
obj.comment.ReplaceCharInline(*L",", *L"."); //parser dont understand , lol
//UE_LOG(LogTemp, Warning, TEXT("start sq %f, txt %s"), obj.Square, *obj.comment);
filtered_grouped.Add(obj);
filtered_grCoords.Add(Fcppcoords(obj.FlatId, obj.flatType, obj.Zone, obj.House, obj.Section));
}
nn++;
}
}
bool UcppFuncLibrary::inRange(float in, float min, float max)
{
return (in >= min) && (in <= max);
}
bool UcppFuncLibrary::inRange(int in, int min, int max)
{
return (in >= min) && (in <= max);
}
bool UcppFuncLibrary::boolGet(TArray<bool> arr, int index)
{
if (arr.Contains(true)) {
if (arr.IsValidIndex(index))return arr[index];
return false;
}
return true;
}
void UcppFuncLibrary::minmaxInRange(TArray<int> arr, int min, int max, int& minid, int& maxid)
{
int temp;
max = (max > (arr.Num() - 1)) ? arr.Num() - 1 : (max < 0 ? 0 : max);
min = (min > (arr.Num() - 1)) ? arr.Num() - 1 : (min < 0 ? 0 : min);
arr.SetNum(max + 1); //delete overmax ids
for (int i = 0; i < min; i++)arr.RemoveAt(0);// delete belowmin ids
UKismetMathLibrary::MinOfIntArray(arr, minid, temp);
minid += min; //return original order
UKismetMathLibrary::MaxOfIntArray(arr, maxid, temp);
maxid += min;
}
void UcppFuncLibrary::minmaxInRange(TArray<float> arr, int min, int max, int& minid, int& maxid)
{
float temp;
max = (max > (arr.Num() - 1)) ? arr.Num() - 1 : (max < 0 ? 0 : max);
min = (min > (arr.Num() - 1)) ? arr.Num() - 1 : (min < 0 ? 0 : min);
arr.SetNum(max + 1); //delete overmax ids
for (int i = 0; i < min; i++)arr.RemoveAt(0);// delete belowmin ids
UKismetMathLibrary::MinOfFloatArray(arr, minid, temp);
minid += min; //return original order
UKismetMathLibrary::MaxOfFloatArray(arr, maxid, temp);
maxid += min;
}