Skip to content

Commit acad0c4

Browse files
committed
Plugin: Snapshot
1 parent 4de3091 commit acad0c4

File tree

7 files changed

+973
-1
lines changed

7 files changed

+973
-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: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
17+
const int NWNX_SNAPSHOT_FILTER_TYPE_TAG = 1;
18+
const int NWNX_SNAPSHOT_FILTER_TYPE_LOCALVAR = 2;
19+
const int NWNX_SNAPSHOT_FILTER_TYPE_CREATURESTATS = 3;
20+
21+
22+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_DEFAULT = 0;
23+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_STRING_REGEX = 1;
24+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCALVAR_INT = 1;
25+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCALVAR_FLOAT = 2;
26+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCALVAR_OBJECT = 3;
27+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_LOCALVAR_STRING = 4;
28+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURESTATS_ABILITY = 1;
29+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURESTATS_ABILITY_MOD = 2;
30+
const int NWNX_SNAPSHOT_FILTER_SUBTYPE_CREATURESTATS_HAS_CLASS = 3;
31+
32+
struct NWNX_Snapshot_Filter
33+
{
34+
int nType;
35+
int nSubtype;
36+
int nComparison;
37+
int bInvert;
38+
39+
int nParam1;
40+
int nParam2;
41+
string sParam1;
42+
string sParam2;
43+
float fParam1;
44+
float fParam2;
45+
object oParam1;
46+
object oParam2;
47+
};
48+
49+
/// @brief Create a snapshot of objects that are within a shape.
50+
/// @param nShape One of SHAPE_*
51+
/// @param fSize Depends on nShape:
52+
/// SHAPE_SPHERE: the radius of the sphere.
53+
/// SHAPE_SPELLCYLINDER: the length of the cylinder. Spell Cylinder's always have a radius of 1.5m.
54+
/// SHAPE_CONE: the widest radius of the cone.
55+
/// SHAPE_SPELLCONE: the length of the cone in the direction of locTarget. Spell cones are always 60 degrees with the origin at OBJECT_SELF.
56+
/// SHAPE_CUBE: half the length of one of the sides of the cube.
57+
/// @param locTarget This is the centre of the effect, usually GetSpellTargetLocation(), or the end of a cylinder or cone.
58+
/// @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.
59+
/// @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.
60+
/// @param vOrigin This is only used for cylinders and cones, and specifies the origin of the effect(normally the spell-caster's position).
61+
/// @param nSnapshotID An optional ID for this snapshot. Useful if you want to nest calls to this function.
62+
/// @return The amount of objects in the snapshot.
63+
int NWNX_Snapshot_CreateAreaShapeSnapshot(int nShape, float fSize, location locTarget, int bLineOfSight = FALSE, int nObjectFilter = OBJECT_TYPE_CREATURE, vector vOrigin = [0.0f,0.0f,0.0f], int nSnapshotID = 0);
64+
65+
/// @brief Create a snapshot of objects that are within an area.
66+
/// @param oArea The area.
67+
/// @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.
68+
/// @param nSnapshotID An optional ID for this snapshot. Useful if you want to nest calls to this function.
69+
/// @return The amount of objects in the snapshot.
70+
int NWNX_Snapshot_CreateAreaSnapshot(object oArea, int nObjectFilter = OBJECT_TYPE_CREATURE, int nSnapshotID = 0);
71+
72+
/// @brief Create a snapshot of objects that are within the whole module.
73+
/// @note Does not include player characters.
74+
/// @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.
75+
/// @param nSnapshotID An optional ID for this snapshot. Useful if you want to nest calls to this function.
76+
/// @return The amount of objects in the snapshot.
77+
int NWNX_Snapshot_CreateModuleSnapshot(int nObjectFilter = OBJECT_TYPE_CREATURE, int nSnapshotID = 0);
78+
79+
/// @brief Gets the object at nIndex from a snapshot.
80+
/// @param nIndex The index of the object to get.
81+
/// @param nSnapshotID An optional ID to get objects from a specific snapshot.
82+
/// @return The object at nIndex or OBJECT_INVALID;
83+
object NWNX_Snapshot_GetObjectFromSnapshot(int nIndex, int nSnapshotID = 0);
84+
85+
/// @brief Prune all invalid objects from a snapshot.
86+
/// @param nSnapshotID An optional ID to prune a specific snapshot.
87+
/// @return The number of objects in the snapshot after pruning.
88+
int NWNX_Snapshot_PruneSnapshot(int nSnapshotID = 0);
89+
90+
/// @brief Clear a snapshot, removing all objects.
91+
/// @param nSnapshotID An optional snapshot ID.
92+
void NWNX_Snapshot_ClearSnapshot(int nSnapshotID = 0);
93+
94+
/// @brief Sort a snapshot by distance to oTarget, ascending.
95+
/// @param oTarget The target.
96+
/// @param nSnapshotID An optional snapshot ID.
97+
void NWNX_Snapshot_SortSnapshotByDistance(object oTarget, int nSnapshotID = 0);
98+
99+
/// @brief Add a filter to nSnapshotID, should be called before creating a snapshot.
100+
/// @param nSnapshotID A snapshot ID.
101+
/// @param strFilter A NWNX_Snapshot_Filter struct.
102+
void NWNX_Snapshot_AddFilter(int nSnapshotID, struct NWNX_Snapshot_Filter strFilter);
103+
104+
/// @brief Remove all filters associated with nSnapshotID.
105+
/// @param nSnapshotID A snapshot ID.
106+
void NWNX_Snapshot_ClearFilters(int nSnapshotID);
107+
108+
/// @}
109+
110+
int NWNX_Snapshot_CreateAreaShapeSnapshot(int nShape, float fSize, location locTarget, int bLineOfSight = FALSE, int nObjectFilter = OBJECT_TYPE_CREATURE, vector vOrigin = [0.0f,0.0f,0.0f], int nSnapshotID = 0)
111+
{
112+
string sFunc = "CreateAreaShapeSnapshot";
113+
114+
object oArea = GetAreaFromLocation(locTarget);
115+
vector vPosition = GetPositionFromLocation(locTarget);
116+
117+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nSnapshotID);
118+
NWNX_PushArgumentFloat(NWNX_Snapshot, sFunc, vOrigin.z);
119+
NWNX_PushArgumentFloat(NWNX_Snapshot, sFunc, vOrigin.y);
120+
NWNX_PushArgumentFloat(NWNX_Snapshot, sFunc, vOrigin.x);
121+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nObjectFilter);
122+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, bLineOfSight);
123+
NWNX_PushArgumentFloat(NWNX_Snapshot, sFunc, vPosition.z);
124+
NWNX_PushArgumentFloat(NWNX_Snapshot, sFunc, vPosition.y);
125+
NWNX_PushArgumentFloat(NWNX_Snapshot, sFunc, vPosition.x);
126+
NWNX_PushArgumentObject(NWNX_Snapshot, sFunc, oArea);
127+
NWNX_PushArgumentFloat(NWNX_Snapshot, sFunc, fSize);
128+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nShape);
129+
NWNX_CallFunction(NWNX_Snapshot, sFunc);
130+
131+
return NWNX_GetReturnValueInt(NWNX_Snapshot, sFunc);
132+
}
133+
134+
int NWNX_Snapshot_CreateAreaSnapshot(object oArea, int nObjectFilter = OBJECT_TYPE_CREATURE, int nSnapshotID = 0)
135+
{
136+
string sFunc = "CreateAreaSnapshot";
137+
138+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nSnapshotID);
139+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nObjectFilter);
140+
NWNX_PushArgumentObject(NWNX_Snapshot, sFunc, oArea);
141+
NWNX_CallFunction(NWNX_Snapshot, sFunc);
142+
143+
return NWNX_GetReturnValueInt(NWNX_Snapshot, sFunc);
144+
}
145+
146+
int NWNX_Snapshot_CreateModuleSnapshot(int nObjectFilter = OBJECT_TYPE_CREATURE, int nSnapshotID = 0)
147+
{
148+
string sFunc = "CreateModuleSnapshot";
149+
150+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nSnapshotID);
151+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nObjectFilter);
152+
NWNX_CallFunction(NWNX_Snapshot, sFunc);
153+
154+
return NWNX_GetReturnValueInt(NWNX_Snapshot, sFunc);
155+
}
156+
157+
object NWNX_Snapshot_GetObjectFromSnapshot(int nIndex, int nSnapshotID = 0)
158+
{
159+
string sFunc = "GetObjectFromSnapshot";
160+
161+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nSnapshotID);
162+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nIndex);
163+
NWNX_CallFunction(NWNX_Snapshot, sFunc);
164+
165+
return NWNX_GetReturnValueObject(NWNX_Snapshot, sFunc);
166+
}
167+
168+
int NWNX_Snapshot_PruneSnapshot(int nSnapshotID = 0)
169+
{
170+
string sFunc = "PruneSnapshot";
171+
172+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nSnapshotID);
173+
NWNX_CallFunction(NWNX_Snapshot, sFunc);
174+
175+
return NWNX_GetReturnValueInt(NWNX_Snapshot, sFunc);
176+
}
177+
178+
void NWNX_Snapshot_ClearSnapshot(int nSnapshotID = 0)
179+
{
180+
string sFunc = "ClearSnapshot";
181+
182+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nSnapshotID);
183+
NWNX_CallFunction(NWNX_Snapshot, sFunc);
184+
}
185+
186+
void NWNX_Snapshot_SortSnapshotByDistance(object oTarget, int nSnapshotID = 0)
187+
{
188+
string sFunc = "SortSnapshotByDistance";
189+
190+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nSnapshotID);
191+
NWNX_PushArgumentObject(NWNX_Snapshot, sFunc, oTarget);
192+
NWNX_CallFunction(NWNX_Snapshot, sFunc);
193+
}
194+
195+
void NWNX_Snapshot_AddFilter(int nSnapshotID, struct NWNX_Snapshot_Filter strFilter)
196+
{
197+
string sFunc = "AddFilter";
198+
199+
NWNX_PushArgumentObject(NWNX_Snapshot, sFunc, strFilter.oParam2);
200+
NWNX_PushArgumentObject(NWNX_Snapshot, sFunc, strFilter.oParam1);
201+
NWNX_PushArgumentFloat(NWNX_Snapshot, sFunc, strFilter.fParam2);
202+
NWNX_PushArgumentFloat(NWNX_Snapshot, sFunc, strFilter.fParam1);
203+
NWNX_PushArgumentString(NWNX_Snapshot, sFunc, strFilter.sParam2);
204+
NWNX_PushArgumentString(NWNX_Snapshot, sFunc, strFilter.sParam1);
205+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, strFilter.nParam2);
206+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, strFilter.nParam1);
207+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, strFilter.bInvert);
208+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, strFilter.nComparison);
209+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, strFilter.nSubtype);
210+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, strFilter.nType);
211+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nSnapshotID);
212+
NWNX_CallFunction(NWNX_Snapshot, sFunc);
213+
}
214+
215+
void NWNX_Snapshot_ClearFilters(int nSnapshotID)
216+
{
217+
string sFunc = "ClearFilters";
218+
219+
NWNX_PushArgumentInt(NWNX_Snapshot, sFunc, nSnapshotID);
220+
NWNX_CallFunction(NWNX_Snapshot, sFunc);
221+
}

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)