testfactions.nss
// 'testfactions'
/*
Utility script for checking how Creatures feel about other Creatures in an
area. It effectively switches the parameters oSource and oTarget for these
functions:
- GetReputation()
- GetIsEnemy(), GetIsNeutral(), GetIsFriend()
- GetNearestCreature(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_*)
HAVE FUN WITH THAT!!!!!
Ie. with the changes, this script returns correct values.
Conclusion: For factions to work correctly in NwN2 invert the Faction Table
(except the Player column because it always returns its own value).
*/
// kevL 2014 apr 20.
// debug to Chat.
void Tell(string sTell)
{
SendMessageToPC(GetFirstPC(FALSE), sTell);
PrintString(sTell);
}
/**
* Prototypes
*/
// Returns TRUE if Source feels REPUTATION_TYPE_* against oTarget.
// - uses switched parameters for GetReputation() ....
int GetIsReputationType(int iRepType, object oTarget, object oSource = OBJECT_SELF);
// A variant of GetNearestCreature() that switches the determination of Target's
// reputation against Source for Source's reputation against Target.
// - it works in accord with the toolset FactionTable!!
object GetNearestCreature_kL(int iType1, int iVal1,
object oSource = OBJECT_SELF, int i = 1,
int iType2 = -1, int iVal2 = -1,
int iType3 = -1, int iVal3 = -1);
// Not used in this script:
// this function corrects the fault in AdjustReputation()
void kL_AdjustReputation(object oTarget, object oSource, int iAdjustment);
/**
* MAIN ***
*/
void main()
{
object oTarget;
int iRepValue;
string sSource;
int i;
object oSource = GetFirstObjectInArea();
while (GetIsObjectValid(oSource))
{
if (GetObjectType(oSource) == OBJECT_TYPE_CREATURE)
{
sSource = GetName(oSource);
if (sSource == "") sSource = "Source";
i = 1;
oTarget = GetNearestObject(OBJECT_TYPE_CREATURE, oSource, i);
while (GetIsObjectValid(oTarget))
{
iRepValue = GetReputation(oTarget, oSource);
Tell("\n. " + sSource + " vs " + GetName(oTarget) + " : " + IntToString(iRepValue));
Tell(". . isEnemy = " + IntToString(GetIsEnemy( oSource, oTarget)));
Tell(". . isNeutral = " + IntToString(GetIsNeutral(oSource, oTarget)));
Tell(". . isFriend = " + IntToString(GetIsFriend( oSource, oTarget)));
oTarget = GetNearestObject(OBJECT_TYPE_CREATURE, oSource, ++i);
}
Tell("\n. " + sSource + "'s near Enemies :");
i = 1;
oTarget = GetNearestCreature_kL(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY, oSource, i);
while (GetIsObjectValid(oTarget))
{
Tell(". . . " + GetName(oTarget));
oTarget = GetNearestCreature_kL(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY, oSource, ++i);
}
Tell("\n. " + sSource + "'s near Neutrals :");
i = 1;
oTarget = GetNearestCreature_kL(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_NEUTRAL, oSource, i);
while (GetIsObjectValid(oTarget))
{
Tell(". . . " + GetName(oTarget));
oTarget = GetNearestCreature_kL(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_NEUTRAL, oSource, ++i);
}
Tell("\n. " + sSource + "'s near Friends :");
i = 1;
oTarget = GetNearestCreature_kL(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_FRIEND, oSource, i);
while (GetIsObjectValid(oTarget))
{
Tell(". . . " + GetName(oTarget));
oTarget = GetNearestCreature_kL(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_FRIEND, oSource, ++i);
}
}
oSource = GetNextObjectInArea();
}
}
/**
* Functions
*/
// A variant of GetNearestCreature() that switches the determination of Target's
// reputation against Source for Source's reputation against Target.
// - it works in accord with the toolset FactionTable!!
object GetNearestCreature_kL(int iType1, int iVal1,
object oSource = OBJECT_SELF, int i = 1,
int iType2 = -1, int iVal2 = -1,
int iType3 = -1, int iVal3 = -1)
{
if ( iType1 != CREATURE_TYPE_REPUTATION
&& iType2 != CREATURE_TYPE_REPUTATION
&& iType3 != CREATURE_TYPE_REPUTATION)
{
return GetNearestCreature(iType1, iVal1, oSource, i, iType2, iVal2, iType3, iVal3);
}
object oTarget;
int
j = 1,
iMatch = 0,
iRepType;
if (iType1 == CREATURE_TYPE_REPUTATION)
{
int bType = FALSE;
iRepType = iVal1;
if (iType2 > -1)
{
bType = TRUE;
iType1 = iType2; iVal1 = iVal2;
iType2 = iType3; iVal2 = iVal3;
oTarget = GetNearestCreature(iType1, iVal1, oSource, j, iType2, iVal2);
}
else
oTarget = GetNearestObject(OBJECT_TYPE_CREATURE, oSource, j);
while (GetIsObjectValid(oTarget))
{
if (GetIsReputationType(iRepType, oTarget, oSource) && ++iMatch == i)
return oTarget;
if (bType)
oTarget = GetNearestCreature(iType1, iVal1, oSource, ++j, iType2, iVal2);
else
oTarget = GetNearestObject(OBJECT_TYPE_CREATURE, oSource, ++j);
}
}
else if (iType2 == CREATURE_TYPE_REPUTATION)
{
iRepType = iVal2;
if (iType3 > -1)
{
iType2 = iType3; iVal2 = iVal3;
}
else
{
iType2 = -1; iVal2 = -1;
}
oTarget = GetNearestCreature(iType1, iVal1, oSource, j, iType2, iVal2);
while (GetIsObjectValid(oTarget))
{
if (GetIsReputationType(iRepType, oTarget, oSource) && ++iMatch == i)
return oTarget;
oTarget = GetNearestCreature(iType1, iVal1, oSource, ++j, iType2, iVal2);
}
}
else if (iType3 == CREATURE_TYPE_REPUTATION)
{
oTarget = GetNearestCreature(iType1, iVal1, oSource, j, iType2, iVal2);
while (GetIsObjectValid(oTarget))
{
if (GetIsReputationType(iVal3, oTarget, oSource) && ++iMatch == i)
return oTarget;
oTarget = GetNearestCreature(iType1, iVal1, oSource, ++j, iType2, iVal2);
}
}
return OBJECT_INVALID;
}
// Returns TRUE if Source feels REPUTATION_TYPE_* against oTarget.
// - uses switched parameters for GetReputation() ....
int GetIsReputationType(int iRepType, object oTarget, object oSource = OBJECT_SELF)
{
if (iRepType == REPUTATION_TYPE_ENEMY)
{
return (GetReputation(oTarget, oSource) < 11);
}
if (iRepType == REPUTATION_TYPE_NEUTRAL)
{
return (GetReputation(oTarget, oSource) > 10
&& GetReputation(oTarget, oSource) < 90);
}
if (iRepType == REPUTATION_TYPE_FRIEND)
{
return (GetReputation(oTarget, oSource) > 89);
}
return FALSE;
}
// Not used in this script:
// this function corrects the fault in AdjustReputation()
void kL_AdjustReputation(object oTarget, object oSource, int iAdjustment)
{
int iRep_source = GetReputation(oTarget, oSource);
int iRep_target = GetReputation(oSource, oTarget);
int iDiff = iRep_source - iRep_target;
iDiff += iAdjustment;
AdjustReputation(oTarget, oSource, iDiff);
}