Skip to content

Commit ec7d841

Browse files
committed
Plugin: Snapshot
1 parent b57a0bc commit ec7d841

File tree

7 files changed

+1285
-1
lines changed

7 files changed

+1285
-1
lines changed

NWNXLib/Utils/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
nwnxlib_add("String.cpp")
1+
nwnxlib_add("String.cpp")
2+
nwnxlib_add("VectorMath.cpp")

NWNXLib/Utils/VectorMath.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "nwnx.hpp"
2+
#include "cmath"
3+
4+
namespace NWNXLib::VectorMath
5+
{
6+
float MagnitudeSquared(const Vector& v)
7+
{
8+
return v.x * v.x + v.y *v.y + v.z * v.z;
9+
}
10+
11+
float Magnitude(const Vector& v)
12+
{
13+
float f = MagnitudeSquared(v);
14+
15+
if (f == 1.0f)
16+
return f;
17+
else if (f <= 0.0f)
18+
return 0.0f;
19+
20+
return std::sqrt(f);
21+
}
22+
23+
float Dot(const Vector& a, const Vector& b)
24+
{
25+
return a.x * b.x + a.y * b.y + a.z * b.z;
26+
}
27+
28+
Vector Add(const Vector& a, const Vector& b)
29+
{
30+
Vector v = a;
31+
v.x += b.x;
32+
v.y += b.y;
33+
v.z += b.z;
34+
return v;
35+
}
36+
37+
Vector Subtract(const Vector& a, const Vector& b)
38+
{
39+
Vector v = a;
40+
v.x -= b.x;
41+
v.y -= b.y;
42+
v.z -= b.z;
43+
return v;
44+
}
45+
46+
Vector Multiply(const Vector& v, const float f)
47+
{
48+
return Vector(v.x * f, v.y * f, v.z * f);
49+
}
50+
51+
Vector Normalize(const Vector &v)
52+
{
53+
float f = MagnitudeSquared(v);
54+
55+
if (f == 1.0f)
56+
return v;
57+
else if (f <= 0.0f)
58+
return Vector(1.0f, 0.0f, 0.0f);
59+
60+
f = std::sqrt(1 / f);
61+
return Multiply(v, f);
62+
}
63+
64+
Vector Lineproject(const Vector &a, const Vector &b, const Vector &c)
65+
{
66+
Vector v = Subtract(b, a);
67+
float f = MagnitudeSquared(v);
68+
69+
if (f)
70+
f = Dot(v, Subtract(c, a)) / f;
71+
72+
return Add(a, Multiply(v, f));
73+
}
74+
}

NWNXLib/nwnx.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ namespace String
173173
std::string Basename(const std::string& path);
174174
}
175175

176+
namespace VectorMath
177+
{
178+
float MagnitudeSquared(const Vector& v);
179+
float Magnitude(const Vector& v);
180+
float Dot(const Vector& a, const Vector& b);
181+
Vector Add(const Vector& a, const Vector& b);
182+
Vector Subtract(const Vector& a, const Vector& b);
183+
Vector Multiply(const Vector& v, float s);
184+
Vector Normalize(const Vector &v);
185+
Vector Lineproject(const Vector &a, const Vector &b, const Vector &c);
186+
}
187+
176188
namespace Utils
177189
{
178190
std::string ObjectIDToString(const ObjectID id);

Plugins/Snapshot/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_plugin(Snapshot "Snapshot.cpp")
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
/// @addtogroup snapshot Snapshot
2+
/// @brief Functions to create cool object snapshots with filters!
3+
/// @{
4+
/// @file nwnx_snapshot.nss
5+
#include "nwnx"
6+
7+
const string NWNX_Snapshot = "NWNX_Snapshot"; ///< @private
8+
9+
const int NWNX_SNAPSHOT_COMPARISON_EQUALS = 0;
10+
const int NWNX_SNAPSHOT_COMPARISON_NOT_EQUALS = 1;
11+
const int NWNX_SNAPSHOT_COMPARISON_GREATER_THAN = 2;
12+
const int NWNX_SNAPSHOT_COMPARISON_LESSER_THAN = 3;
13+
const int NWNX_SNAPSHOT_COMPARISON_GREATER_THAN_OR_EQUALS = 4;
14+
const int NWNX_SNAPSHOT_COMPARISON_LESSER_THAN_OR_EQUALS = 5;
15+
16+
const int NWNX_SNAPSHOT_FILTER_TYPE_TAG = 1;
17+
const int NWNX_SNAPSHOT_FILTER_TYPE_LOCALVAR = 2;
18+
const int NWNX_SNAPSHOT_FILTER_TYPE_CREATURE = 3;
19+
const int NWNX_SNAPSHOT_FILTER_TYPE_LOCATION = 4;
20+
const int NWNX_SNAPSHOT_FILTER_TYPE_APPEARANCE = 5;
21+
const int NWNX_SNAPSHOT_FILTER_TYPE_EFFECT = 6;
22+
const int NWNX_SNAPSHOT_FILTER_TYPE_CUSTOM = 7;
23+
const int NWNX_SNAPSHOT_FILTER_TYPE_REPUTATION = 8;
24+
25+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_DEFAULT = 0;
26+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_STRING_REGEX = 1;
27+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCALVAR_INT = 1;
28+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCALVAR_FLOAT = 2;
29+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCALVAR_OBJECT = 3;
30+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCALVAR_STRING = 4;
31+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_ABILITY = 1;
32+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_ABILITY_MOD = 2;
33+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_HAS_CLASS = 3;
34+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_HAS_CLASS_OF_LEVEL = 4;
35+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_IS_PLAYER_CHARACTER = 5;
36+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_RACIAL_TYPE = 6;
37+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_LEVEL = 7;
38+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_HAS_FEAT = 8;
39+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_EXPERIENCE = 9;
40+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_GOLD = 10;
41+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_POISONED = 11;
42+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_DISEASED = 12;
43+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_SOUND_SET = 13;
44+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURE_PERCEPTION = 14;
45+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCATION_AREA = 1;
46+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCATION_X = 2;
47+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCATION_Y = 3;
48+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCATION_Z = 4;
49+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCATION_ORIENTATION = 5;
50+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_APPEARANCE_TYPE_CREATURE = 1;
51+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_APPEARANCE_TYPE_PLACEABLE = 2;
52+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_EFFECT_HAS_SPELL_EFFECT = 1;
53+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_EFFECT_HAS_FEAT_EFFECT = 2;
54+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CUSTOM_RUN_CONDITIONAL_SCRIPT = 1;
55+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_REPUTATION_GET_REPUTATION = 1;
56+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_REPUTATION_IS_FRIEND = 2;
57+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_REPUTATION_IS_NEUTRAL = 3;
58+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_REPUTATION_IS_ENEMY = 4;
59+
60+
struct NWNX_Snapshot_Filter
61+
{
62+
int nType;
63+
int nSubtype;
64+
int nComparison;
65+
int bInvert;
66+
67+
int nParam1;
68+
int nParam2;
69+
string sParam1;
70+
string sParam2;
71+
float fParam1;
72+
float fParam2;
73+
object oParam1;
74+
object oParam2;
75+
};
76+
77+
/// @brief Create a snapshot of objects that are within a shape.
78+
/// @note Filters for nSnapshotID=0 will be automatically cleared after this call.
79+
/// @param nShape One of SHAPE_*
80+
/// @param fSize Depends on nShape:
81+
/// SHAPE_SPHERE: the radius of the sphere.
82+
/// SHAPE_SPELLCYLINDER: the length of the cylinder. Spell Cylinder's always have a radius of 1.5m.
83+
/// SHAPE_CONE: the widest radius of the cone.
84+
/// SHAPE_SPELLCONE: the length of the cone in the direction of locTarget. Spell cones are always 60 degrees with the origin at OBJECT_SELF.
85+
/// SHAPE_CUBE: half the length of one of the sides of the cube.
86+
/// @param locTarget This is the centre of the effect, usually GetSpellTargetLocation(), or the end of a cylinder or cone.
87+
/// @param bLineOfSight Controls whether to do a line-of-sight check on the object returned. The line of sight check is done from origin to target object at a height 1m above the ground.
88+
/// @param nObjectFilter This allows you to filter out undesired object types, using bitwise "or". For example, to return only creatures and doors, the value for this parameter would be OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR.
89+
/// @param vOrigin This is only used for cylinders and cones, and specifies the origin of the effect(normally the spell-caster's position).
90+
/// @param nSnapshotID An optional ID for this snapshot. Useful if you want to nest calls to this function.
91+
/// @return The amount of objects in the snapshot.
92+
int NWNX_Snapshot_CreateAreaShapeSnapshot(int nShape, float fSize, location locTarget, int bLineOfSight, int nObjectFilter, vector vOrigin, int nSnapshotID = 0);
93+
94+
/// @brief Create a snapshot of objects that are within an area.
95+
/// @note Filters for nSnapshotID=0 will be automatically cleared after this call.
96+
/// @param oArea The area.
97+
/// @param nObjectFilter This allows you to filter out undesired object types, using bitwise "or". For example, to return only creatures and doors, the value for this parameter would be OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR.
98+
/// @param nSnapshotID An optional ID for this snapshot. Useful if you want to nest calls to this function.
99+
/// @return The amount of objects in the snapshot.
100+
int NWNX_Snapshot_CreateAreaSnapshot(object oArea, int nObjectFilter = OBJECT_TYPE_CREATURE, int nSnapshotID = 0);
101+
102+
/// @brief Create a snapshot of objects that are within the whole module.
103+
/// @note Filters for nSnapshotID=0 will be automatically cleared after this call.
104+
/// @note Does not include player characters.
105+
/// @param nObjectFilter This allows you to filter out undesired object types, using bitwise "or". For example, to return only creatures and doors, the value for this parameter would be OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR.
106+
/// @param nSnapshotID An optional ID for this snapshot. Useful if you want to nest calls to this function.
107+
/// @return The amount of objects in the snapshot.
108+
int NWNX_Snapshot_CreateModuleSnapshot(int nObjectFilter = OBJECT_TYPE_CREATURE, int nSnapshotID = 0);
109+
110+
/// @brief Create a snapshot of player objects.
111+
/// @note Filters for nSnapshotID=0 will be automatically cleared after this call.
112+
/// @param nSnapshotID An optional ID for this snapshot. Useful if you want to nest calls to this function.
113+
/// @return The amount of objects in the snapshot.
114+
int NWNX_Snapshot_CreatePlayerSnapshot(int nSnapshotID = 0);
115+
116+
/// @brief Gets the object at nIndex from a snapshot.
117+
/// @param nIndex The index of the object to get.
118+
/// @param nSnapshotID An optional ID to get objects from a specific snapshot.
119+
/// @return The object at nIndex or OBJECT_INVALID;
120+
object NWNX_Snapshot_GetObjectFromSnapshot(int nIndex, int nSnapshotID = 0);
121+
122+
/// @brief Prune all invalid objects from a snapshot.
123+
/// @param nSnapshotID An optional ID to prune a specific snapshot.
124+
/// @return The number of objects in the snapshot after pruning.
125+
int NWNX_Snapshot_PruneSnapshot(int nSnapshotID = 0);
126+
127+
/// @brief Clear a snapshot, removing all objects.
128+
/// @param nSnapshotID An optional snapshot ID to clear a specific snapshot.
129+
void NWNX_Snapshot_ClearSnapshot(int nSnapshotID = 0);
130+
131+
/// @brief Sort a snapshot by distance to oTarget.
132+
/// @note When sorting by descending, objects in a different area than oTarget will be at the front.
133+
/// @param oTarget The target.
134+
/// @param bAscending TRUE to sort ascending, FALSE for descending.
135+
/// @param nSnapshotID An optional snapshot ID.
136+
void NWNX_Snapshot_SortSnapshotByDistance(object oTarget, int bAscending = TRUE, int nSnapshotID = 0);
137+
138+
/// @brief Add a filter to nSnapshotID, should be called before creating a snapshot.
139+
/// @param nSnapshotID A snapshot ID.
140+
/// @param strFilter A NWNX_Snapshot_Filter struct.
141+
void NWNX_Snapshot_AddFilter(int nSnapshotID, struct NWNX_Snapshot_Filter strFilter);
142+
143+
/// @brief Remove all filters associated with nSnapshotID.
144+
/// @param nSnapshotID A snapshot ID.
145+
void NWNX_Snapshot_ClearFilters(int nSnapshotID);
146+
147+
/// @}
148+
149+
int NWNX_Snapshot_CreateAreaShapeSnapshot(int nShape, float fSize, location locTarget, int bLineOfSight, int nObjectFilter, vector vOrigin, int nSnapshotID = 0)
150+
{
151+
object oArea = GetAreaFromLocation(locTarget);
152+
vector vPosition = GetPositionFromLocation(locTarget);
153+
154+
NWNX_PushArgumentInt(nSnapshotID);
155+
NWNX_PushArgumentFloat(vOrigin.z);
156+
NWNX_PushArgumentFloat(vOrigin.y);
157+
NWNX_PushArgumentFloat(vOrigin.x);
158+
NWNX_PushArgumentInt(nObjectFilter);
159+
NWNX_PushArgumentInt(bLineOfSight);
160+
NWNX_PushArgumentFloat(vPosition.z);
161+
NWNX_PushArgumentFloat(vPosition.y);
162+
NWNX_PushArgumentFloat(vPosition.x);
163+
NWNX_PushArgumentObject(oArea);
164+
NWNX_PushArgumentFloat(fSize);
165+
NWNX_PushArgumentInt(nShape);
166+
NWNX_CallFunction(NWNX_Snapshot, "CreateAreaShapeSnapshot");
167+
168+
return NWNX_GetReturnValueInt();
169+
}
170+
171+
int NWNX_Snapshot_CreateAreaSnapshot(object oArea, int nObjectFilter = OBJECT_TYPE_CREATURE, int nSnapshotID = 0)
172+
{
173+
NWNX_PushArgumentInt(nSnapshotID);
174+
NWNX_PushArgumentInt(nObjectFilter);
175+
NWNX_PushArgumentObject(oArea);
176+
NWNX_CallFunction(NWNX_Snapshot, "CreateAreaSnapshot");
177+
178+
return NWNX_GetReturnValueInt();
179+
}
180+
181+
int NWNX_Snapshot_CreateModuleSnapshot(int nObjectFilter = OBJECT_TYPE_CREATURE, int nSnapshotID = 0)
182+
{
183+
NWNX_PushArgumentInt(nSnapshotID);
184+
NWNX_PushArgumentInt(nObjectFilter);
185+
NWNX_CallFunction(NWNX_Snapshot, "CreateModuleSnapshot");
186+
187+
return NWNX_GetReturnValueInt();
188+
}
189+
190+
int NWNX_Snapshot_CreatePlayerSnapshot(int nSnapshotID = 0)
191+
{
192+
NWNX_PushArgumentInt(nSnapshotID);
193+
NWNX_CallFunction(NWNX_Snapshot, "CreatePlayerSnapshot");
194+
195+
return NWNX_GetReturnValueInt();
196+
}
197+
198+
object NWNX_Snapshot_GetObjectFromSnapshot(int nIndex, int nSnapshotID = 0)
199+
{
200+
NWNX_PushArgumentInt(nSnapshotID);
201+
NWNX_PushArgumentInt(nIndex);
202+
NWNX_CallFunction(NWNX_Snapshot, "GetObjectFromSnapshot");
203+
204+
return NWNX_GetReturnValueObject();
205+
}
206+
207+
int NWNX_Snapshot_PruneSnapshot(int nSnapshotID = 0)
208+
{
209+
NWNX_PushArgumentInt(nSnapshotID);
210+
NWNX_CallFunction(NWNX_Snapshot, "PruneSnapshot");
211+
212+
return NWNX_GetReturnValueInt();
213+
}
214+
215+
void NWNX_Snapshot_ClearSnapshot(int nSnapshotID = 0)
216+
{
217+
NWNX_PushArgumentInt(nSnapshotID);
218+
NWNX_CallFunction(NWNX_Snapshot, "ClearSnapshot");
219+
}
220+
221+
void NWNX_Snapshot_SortSnapshotByDistance(object oTarget, int bAscending = TRUE, int nSnapshotID = 0)
222+
{
223+
NWNX_PushArgumentInt(nSnapshotID);
224+
NWNX_PushArgumentInt(bAscending);
225+
NWNX_PushArgumentObject(oTarget);
226+
NWNX_CallFunction(NWNX_Snapshot, "SortSnapshotByDistance");
227+
}
228+
229+
void NWNX_Snapshot_AddFilter(int nSnapshotID, struct NWNX_Snapshot_Filter strFilter)
230+
{
231+
NWNX_PushArgumentObject(strFilter.oParam2);
232+
NWNX_PushArgumentObject(strFilter.oParam1);
233+
NWNX_PushArgumentFloat(strFilter.fParam2);
234+
NWNX_PushArgumentFloat(strFilter.fParam1);
235+
NWNX_PushArgumentString(strFilter.sParam2);
236+
NWNX_PushArgumentString(strFilter.sParam1);
237+
NWNX_PushArgumentInt(strFilter.nParam2);
238+
NWNX_PushArgumentInt(strFilter.nParam1);
239+
NWNX_PushArgumentInt(strFilter.bInvert);
240+
NWNX_PushArgumentInt(strFilter.nComparison);
241+
NWNX_PushArgumentInt(strFilter.nSubtype);
242+
NWNX_PushArgumentInt(strFilter.nType);
243+
NWNX_PushArgumentInt(nSnapshotID);
244+
NWNX_CallFunction(NWNX_Snapshot, "AddFilter");
245+
}
246+
247+
void NWNX_Snapshot_ClearFilters(int nSnapshotID)
248+
{
249+
NWNX_PushArgumentInt(nSnapshotID);
250+
NWNX_CallFunction(NWNX_Snapshot, "ClearFilters");
251+
}

Plugins/Snapshot/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@page snapshot Readme
2+
@ingroup snapshot
3+
4+
Functions to create cool object snapshots with filters!

0 commit comments

Comments
 (0)