Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c05b278

Browse files
committed
Fix params, change GetNumRefs(InCell), port distance checks to the Matcher classes
1 parent 944b896 commit c05b278

4 files changed

Lines changed: 132 additions & 61 deletions

File tree

nvse/nvse/Commands_MiscRef.cpp

Lines changed: 107 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ class RefMatcherARefr
318318
{
319319
bool m_includeTaken;
320320
TESObjectREFR* m_refr;
321+
321322
public:
322323
RefMatcherARefr(bool includeTaken, TESObjectREFR* refr) : m_includeTaken(includeTaken), m_refr(refr)
323324
{ }
@@ -335,64 +336,119 @@ class RefMatcherARefr
335336

336337
class RefMatcherAnyForm
337338
{
338-
bool m_includeTaken;
339+
bool m_includeTaken; //if true, ignore.
340+
TESObjectREFR* m_distanceRef; //if null, ignore distance check.
341+
float m_maxDistance; //if 0, ignore.
342+
339343
public:
340-
RefMatcherAnyForm(bool includeTaken) : m_includeTaken(includeTaken)
341-
{ }
344+
RefMatcherAnyForm(bool includeTaken) : m_includeTaken(includeTaken), m_maxDistance(0), m_distanceRef(nullptr)
345+
{}
342346

343-
bool Accept(const TESObjectREFR* refr)
347+
RefMatcherAnyForm(bool includeTaken, TESObjectREFR* distanceRef, float maxDistance):
348+
m_includeTaken(includeTaken), m_distanceRef(distanceRef), m_maxDistance(maxDistance)
349+
{}
350+
351+
bool Accept(const TESObjectREFR* refr) const
344352
{
345-
if (m_includeTaken || !(refr->IsTaken()))
346-
return true;
347-
else
353+
if (!m_includeTaken && refr->IsTaken())
348354
return false;
355+
356+
if (refr->baseForm->refID == 7) //exclude player
357+
return false;
358+
359+
if (m_distanceRef && m_maxDistance > 0)
360+
{
361+
if (GetDistance3D(m_distanceRef, refr) > m_maxDistance)
362+
{
363+
return false;
364+
}
365+
}
366+
367+
return true;
349368
}
350369
};
351370

352371
class RefMatcherFormType
353372
{
354373
UInt32 m_formType;
355374
bool m_includeTaken;
375+
TESObjectREFR* m_distanceRef = nullptr; //if null, ignore distance check.
376+
float m_maxDistance = 0; //if 0, ignore.
377+
356378
public:
357379
RefMatcherFormType(UInt32 formType, bool includeTaken) : m_formType(formType), m_includeTaken(includeTaken)
358-
{ }
380+
{}
359381

360-
bool Accept(const TESObjectREFR* refr)
382+
RefMatcherFormType(UInt32 formType, bool includeTaken, TESObjectREFR* distanceRef, float maxDistance):
383+
m_formType(formType), m_includeTaken(includeTaken), m_distanceRef(distanceRef), m_maxDistance(maxDistance)
384+
{}
385+
386+
bool Accept(const TESObjectREFR* refr) const
361387
{
362388
if (!m_includeTaken && refr->IsTaken())
363389
return false;
364-
else if (refr->baseForm->typeID == m_formType && refr->baseForm->refID != 7) //exclude player for kFormType_TESNPC
365-
return true;
366-
else
390+
391+
if (refr->baseForm->typeID != m_formType || refr->baseForm->refID == 7) //exclude player for kFormType_TESNPC
367392
return false;
393+
394+
if (m_distanceRef && m_maxDistance > 0)
395+
{
396+
if (GetDistance3D(m_distanceRef, refr) > m_maxDistance)
397+
{
398+
return false;
399+
}
400+
}
401+
402+
return true;
368403
}
369404
};
370405

371406
class RefMatcherActor
372407
{
408+
TESObjectREFR* m_distanceRef = nullptr; //if null, ignore distance check.
409+
float m_maxDistance = 0; //if 0, ignore.
410+
373411
public:
374-
RefMatcherActor()
375-
{ }
376-
377-
bool Accept(const TESObjectREFR* refr)
412+
RefMatcherActor() = default;
413+
RefMatcherActor(TESObjectREFR* distanceRef, float maxDistance):
414+
m_distanceRef(distanceRef), m_maxDistance(maxDistance)
415+
{}
416+
417+
bool Accept(const TESObjectREFR* refr) const
378418
{
379-
if (refr->baseForm->typeID == kFormType_TESCreature)
380-
return true;
381-
else if (refr->baseForm->typeID == kFormType_TESNPC && refr->baseForm->refID != 7) //exclude the player
382-
return true;
383-
else
419+
if (refr->baseForm->typeID != kFormType_TESCreature
420+
&& (refr->baseForm->typeID != kFormType_TESNPC || refr->baseForm->refID == 7)) //exclude the player
421+
{
384422
return false;
423+
}
424+
425+
if (m_distanceRef && m_maxDistance > 0)
426+
{
427+
if (GetDistance3D(m_distanceRef, refr) > m_maxDistance)
428+
{
429+
return false;
430+
}
431+
}
432+
433+
return true;
385434
}
386435
};
387436

388437
class RefMatcherItem
389438
{
390439
bool m_includeTaken;
440+
TESObjectREFR* m_distanceRef = nullptr; //if null, ignore distance check.
441+
float m_maxDistance = 0; //if 0, ignore.
442+
391443
public:
392444
RefMatcherItem(bool includeTaken) : m_includeTaken(includeTaken)
393-
{ }
445+
{ }
394446

395-
bool Accept(const TESObjectREFR* refr)
447+
RefMatcherItem(bool includeTaken, TESObjectREFR* distanceRef, float maxDistance) :
448+
m_includeTaken(includeTaken), m_distanceRef(distanceRef), m_maxDistance(maxDistance)
449+
{ }
450+
451+
bool Accept(const TESObjectREFR* refr) const
396452
{
397453
if (!m_includeTaken && refr->IsTaken())
398454
return false;
@@ -409,15 +465,25 @@ class RefMatcherItem
409465
case kFormType_TESKey:
410466
case kFormType_AlchemyItem:
411467
case kFormType_TESObjectARMA:
412-
return true;
468+
break;
413469

414470
case kFormType_TESObjectLIGH:
415-
TESObjectLIGH* light = DYNAMIC_CAST(refr->baseForm, TESForm, TESObjectLIGH);
416-
if (light)
471+
if (TESObjectLIGH* light = DYNAMIC_CAST(refr->baseForm, TESForm, TESObjectLIGH))
417472
if (light->icon.ddsPath.m_dataLen) //temp hack until I find canCarry flag on TESObjectLIGH
418-
return true;
473+
break;
474+
default:
475+
return false;
419476
}
420-
return false;
477+
478+
if (m_distanceRef && m_maxDistance > 0)
479+
{
480+
if (GetDistance3D(m_distanceRef, refr) > m_maxDistance)
481+
{
482+
return false;
483+
}
484+
}
485+
486+
return true;
421487
}
422488
};
423489

@@ -605,25 +671,26 @@ static bool GetNumRefs_Execute(COMMAND_ARGS, bool bUsePlayerCell = true)
605671
SInt32 cellDepth = -127;
606672
UInt32 includeTakenRefs = 0;
607673
double uGrid = 0;
674+
float maxDistance = 0;
608675

609676
PlayerCharacter* pc = PlayerCharacter::GetSingleton();
610677
if (!pc || !(pc->parentCell))
611678
return true; //avoid crash when these functions called in main menu before parentCell instantiated
612679

613680
TESObjectCELL* cell = NULL;
614681
if (bUsePlayerCell)
615-
if (ExtractArgs(EXTRACT_ARGS, &formType, &cellDepth, &includeTakenRefs))
682+
if (ExtractArgs(EXTRACT_ARGS, &formType, &cellDepth, &includeTakenRefs, &maxDistance))
616683
cell = pc->parentCell;
617684
else
618685
return true;
619686
else
620-
if (!ExtractArgs(EXTRACT_ARGS, &cell, &formType, &cellDepth, &includeTakenRefs))
687+
if (!ExtractArgs(EXTRACT_ARGS, &cell, &formType, &cellDepth, &includeTakenRefs, &maxDistance))
621688
return true;
622689

623690
if (!cell)
624691
return true;
625692

626-
bool bIncludeTakenRefs = includeTakenRefs ? true : false;
693+
bool const bIncludeTakenRefs = includeTakenRefs ? true : false;
627694
if (cellDepth == -127)
628695
cellDepth = 0;
629696
else if (cellDepth == -1)
@@ -641,16 +708,16 @@ static bool GetNumRefs_Execute(COMMAND_ARGS, bool bUsePlayerCell = true)
641708
switch (formType)
642709
{
643710
case 0:
644-
*result += refList.CountIf(RefMatcherAnyForm(bIncludeTakenRefs));
711+
*result += refList.CountIf(RefMatcherAnyForm(bIncludeTakenRefs, thisObj, maxDistance));
645712
break;
646713
case 200:
647-
*result += refList.CountIf(RefMatcherActor());
714+
*result += refList.CountIf(RefMatcherActor(thisObj, maxDistance));
648715
break;
649716
case 201:
650-
*result += refList.CountIf(RefMatcherItem(bIncludeTakenRefs));
717+
*result += refList.CountIf(RefMatcherItem(bIncludeTakenRefs, thisObj, maxDistance));
651718
break;
652719
default:
653-
*result += refList.CountIf(RefMatcherFormType(formType, bIncludeTakenRefs));
720+
*result += refList.CountIf(RefMatcherFormType(formType, bIncludeTakenRefs, thisObj, maxDistance));
654721
}
655722
info.NextCell();
656723
}
@@ -697,9 +764,6 @@ bool GetRefs_Execute(COMMAND_ARGS, bool bUsePlayerCell = true)
697764
if (!cell)
698765
return true;
699766

700-
if (!thisObj)
701-
maxDistance = 0;
702-
703767
bool const bIncludeTakenRefs = includeTakenRefs ? true : false;
704768
if (cellDepth == -127)
705769
cellDepth = 0;
@@ -717,40 +781,33 @@ bool GetRefs_Execute(COMMAND_ARGS, bool bUsePlayerCell = true)
717781
const TESObjectCELL::RefList& refList = info.curCell->objectList;
718782
for (TESObjectCELL::RefList::Iterator iter = refList.Begin(); !iter.End(); ++iter)
719783
{
720-
TESObjectREFR* pRefr = iter.Get();
721-
if (pRefr)
784+
if (TESObjectREFR* const pRefr = iter.Get())
722785
{
723-
if (maxDistance <= 0
724-
|| GetDistance3D(thisObj, pRefr) > maxDistance)
725-
{
726-
continue;
727-
}
728-
729786
switch (formType)
730787
{
731788
case 0:
732-
if (RefMatcherAnyForm(bIncludeTakenRefs).Accept(pRefr))
789+
if (RefMatcherAnyForm(bIncludeTakenRefs, thisObj, maxDistance).Accept(pRefr))
733790
{
734791
arr->SetElementFormID(arrIndex, pRefr->refID);
735792
arrIndex += 1;
736793
}
737794
break;
738795
case 200:
739-
if (RefMatcherActor().Accept(pRefr))
796+
if (RefMatcherActor(thisObj, maxDistance).Accept(pRefr))
740797
{
741798
arr->SetElementFormID(arrIndex, pRefr->refID);
742799
arrIndex += 1;
743800
}
744801
break;
745802
case 201:
746-
if (RefMatcherItem(bIncludeTakenRefs).Accept(pRefr))
803+
if (RefMatcherItem(bIncludeTakenRefs, thisObj, maxDistance).Accept(pRefr))
747804
{
748805
arr->SetElementFormID(arrIndex, pRefr->refID);
749806
arrIndex += 1;
750807
}
751808
break;
752809
default:
753-
if (RefMatcherFormType(formType, bIncludeTakenRefs).Accept(pRefr))
810+
if (RefMatcherFormType(formType, bIncludeTakenRefs, thisObj, maxDistance).Accept(pRefr))
754811
{
755812
arr->SetElementFormID(arrIndex, pRefr->refID);
756813
arrIndex += 1;

nvse/nvse/Commands_MiscRef.h

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,25 @@ DEFINE_COMMAND(GetParentCellOwner, returns the owner of the cell of the calling
1515
DEFINE_COMMAND(GetOwningFactionRequiredRank, returns the required rank for ownership of the calling reference, 1, 0, NULL);
1616
DEFINE_COMMAND(GetParentCellOwningFactionRequiredRank, returns the required rank for ownership of the cell of the calling reference, 1, 0, NULL);
1717

18-
static ParamInfo kParams_GetFirstRef[4] =
18+
static ParamInfo kParams_GetFirstRef[3] =
19+
{
20+
{ "form type", kParamType_Integer, 1 },
21+
{ "cell depth", kParamType_Integer, 1 },
22+
{ "include taken refs", kParamType_Integer, 1 },
23+
};
24+
25+
static ParamInfo kParams_GetRefs[4] =
1926
{
2027
{ "form type", kParamType_Integer, 1 },
2128
{ "cell depth", kParamType_Integer, 1 },
2229
{ "include taken refs", kParamType_Integer, 1 },
2330
{ "max distance", kParamType_Float, 1 },
2431
};
2532

26-
DEFINE_COMMAND(GetFirstRef, returns the first reference of the specified type in the current cell, 0, 4, kParams_GetFirstRef);
33+
DEFINE_COMMAND(GetFirstRef, returns the first reference of the specified type in the current cell, 0, 3, kParams_GetFirstRef);
2734
DEFINE_COMMAND(GetNextRef, returns the next reference of a given type in the current cell, 0, 0, NULL);
28-
DEFINE_COMMAND(GetNumRefs, returns the number of references of a given type in the current cell, 0, 3, kParams_GetFirstRef);
29-
DEFINE_COMMAND(GetRefs, returns an array of references of a given type in the current cell, 0, 3, kParams_GetFirstRef);
35+
DEFINE_COMMAND(GetNumRefs, returns the number of references of a given type in the current cell, 0, 4, kParams_GetRefs);
36+
DEFINE_COMMAND(GetRefs, returns an array of references of a given type in the current cell, 0, 4, kParams_GetRefs);
3037

3138
static ParamInfo kParams_GetInGrid[3] =
3239
{
@@ -37,19 +44,26 @@ static ParamInfo kParams_GetInGrid[3] =
3744

3845
DEFINE_COMMAND(GetInGrid, returns if a specific reference is in the current cell, 0, 3, kParams_GetInGrid);
3946

40-
static ParamInfo kParams_GetFirstRefInCell[5] =
47+
static ParamInfo kParams_GetFirstRefInCell[4] =
4148
{
4249
{ "cell", kParamType_Cell, 0 },
4350
{ "form type", kParamType_Integer, 1 },
4451
{ "cell depth", kParamType_Integer, 1 },
4552
{ "include taken refs", kParamType_Integer, 1 },
46-
{ "max distance", kParamType_Float, 1 },
53+
};
4754

55+
static ParamInfo kParams_GetRefsInCell[5] =
56+
{
57+
{ "cell", kParamType_Cell, 0 },
58+
{ "form type", kParamType_Integer, 1 },
59+
{ "cell depth", kParamType_Integer, 1 },
60+
{ "include taken refs", kParamType_Integer, 1 },
61+
{ "max distance", kParamType_Float, 1 },
4862
};
4963

50-
DEFINE_COMMAND(GetFirstRefInCell, returns the first reference of the specified type in the specified cell, 0, 5, kParams_GetFirstRefInCell);
51-
DEFINE_COMMAND(GetNumRefsInCell, returns the number of references of a given type in the specified cell, 0, 4, kParams_GetFirstRefInCell);
52-
DEFINE_COMMAND(GetRefsInCell, returns an array of references of a given type in the specified cell, 0, 4, kParams_GetFirstRefInCell);
64+
DEFINE_COMMAND(GetFirstRefInCell, returns the first reference of the specified type in the specified cell, 0, 4, kParams_GetFirstRefInCell);
65+
DEFINE_COMMAND(GetNumRefsInCell, returns the number of references of a given type in the specified cell, 0, 5, kParams_GetRefsInCell);
66+
DEFINE_COMMAND(GetRefsInCell, returns an array of references of a given type in the specified cell, 0, 5, kParams_GetRefsInCell);
5367

5468
static ParamInfo kParams_GetInGridInCell[4] =
5569
{

nvse/nvse/GameObjects.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ TESForm* GetPermanentBaseForm(TESObjectREFR* thisObj) // For LevelledForm, find
209209
}
210210

211211
// Taken from JIP LN NVSE.
212-
__declspec(naked) float __vectorcall GetDistance3D(TESObjectREFR* ref1, TESObjectREFR* ref2)
212+
__declspec(naked) float __vectorcall GetDistance3D(const TESObjectREFR* ref1, const TESObjectREFR* ref2)
213213
{
214214
__asm
215215
{

nvse/nvse/GameObjects.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class TESObjectREFR : public TESForm
172172
TESForm* GetPermanentBaseForm(TESObjectREFR* thisObj); // For LevelledForm, find real baseForm, not temporary one.
173173

174174
// Taken from JIP LN NVSE.
175-
float __vectorcall GetDistance3D(TESObjectREFR* ref1, TESObjectREFR* ref2);
175+
float __vectorcall GetDistance3D(const TESObjectREFR* ref1, const TESObjectREFR* ref2);
176176

177177
STATIC_ASSERT(offsetof(TESObjectREFR, baseForm) == 0x020);
178178
STATIC_ASSERT(offsetof(TESObjectREFR, extraDataList) == 0x044);

0 commit comments

Comments
 (0)