@@ -318,6 +318,7 @@ class RefMatcherARefr
318318{
319319 bool m_includeTaken;
320320 TESObjectREFR* m_refr;
321+
321322public:
322323 RefMatcherARefr (bool includeTaken, TESObjectREFR* refr) : m_includeTaken(includeTaken), m_refr(refr)
323324 { }
@@ -335,64 +336,119 @@ class RefMatcherARefr
335336
336337class 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+
339343public:
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
352371class 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+
356378public:
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
371406class RefMatcherActor
372407{
408+ TESObjectREFR* m_distanceRef = nullptr ; // if null, ignore distance check.
409+ float m_maxDistance = 0 ; // if 0, ignore.
410+
373411public:
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
388437class 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+
391443public:
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 ;
0 commit comments