Files
fs2open.github.com/code/scripting/api/objs/model_path.cpp
T
Goober5000andClaude Fable 5.1 37b0b6ea82 Standardize isValid across the Lua API by making the types auto-generate it (#7761)
* Automatically generate the isValid() Lua API function for handle types

If an ADE object's stored type has an isValid() member, ade_obj now registers an isValid function for it automatically.  An explicit ADE_FUNC(isValid, ...) declared after the ADE_OBJ replaces the generated one, so types with special semantics (e.g. sound) keep their own.  The 67 hand-written functions that merely delegated to the member are removed, and 26 types that previously exposed no isValid() gain one.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* Add ADE_OBJ_VALIDATOR for index-based Lua handles

Int-index handles (shipclass, weaponclass, team, etc.) can now declare their validity predicate with a single ADE_OBJ_VALIDATOR or ADE_OBJ_VALIDATOR_RANGE line after the ADE_OBJ.  The registered validator drives the generated isValid() function and is also honored by ade_set_args when the mod table asks for nil instead of invalid objects, which previously never applied to these handles.  The 19 hand-written range-check functions are replaced, and loadout_ship, default_primary, default_secondary, and support_rearm_pool_team gain isValid().  Add a validator to HudGauge while we're at it.

Also fully qualify the scripting namespace in LuaConvert.h so its templates can be instantiated from the unit tests.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
2026-09-08 10:50:53 -04:00

138 lines
3.9 KiB
C++

//
//
#include "model_path.h"
#include "vecmath.h"
namespace scripting {
namespace api {
struct model_path_point_h {
const model_path_h* parent = nullptr;
vec3d point{};
float radius = -1.0f;
model_path_point_h() = default;
model_path_point_h(const model_path_h* _parent, const vec3d& _point, float _radius)
: parent(_parent), point(_point), radius(_radius)
{
}
bool isValid() const { return parent != nullptr && radius > 0.0f; }
};
ADE_OBJ(l_ModelPathPoint, model_path_point_h, "modelpathpoint", "Point in a model path");
ADE_VIRTVAR(Position, l_ModelPathPoint, "vector", "The current, global position of this path point.", "vector",
"The current position of the point.")
{
model_path_point_h* p = nullptr;
if (!ade_get_args(L, "o", l_ModelPathPoint.GetPtr(&p)))
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if (!p->isValid()) {
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
}
// A submodel is only valid if the object is a ship so this is safe
auto objp = p->parent->subsys.objh.objp();
auto shipp = &Ships[objp->instance];
auto pmi = model_get_instance(shipp->model_instance_num);
auto pm = model_get(pmi->model_num);
vec3d world_pos;
model_instance_local_to_global_point(&world_pos, &p->point, pm, pmi, 0, &objp->orient, &objp->pos);
return ade_set_error(L, "o", l_Vector.Set(world_pos));
}
ADE_VIRTVAR(Radius, l_ModelPathPoint, "number", "The radius of the path point.", "number", "The radius of the point.")
{
model_path_point_h* p = nullptr;
if (!ade_get_args(L, "o", l_ModelPathPoint.GetPtr(&p)))
return ade_set_error(L, "f", -1.0f);
if (!p->isValid()) {
return ade_set_error(L, "f", -1.0f);
}
return ade_set_error(L, "f", p->radius);
}
model_path_h::model_path_h() = default;
model_path_h::model_path_h(const ship_subsys_h& _subsys, const model_path& _path) : subsys(_subsys)
{
name = _path.name;
parent_submodel = _path.parent_submodel;
verts.reserve(_path.nverts);
for (auto i = 0; i < _path.nverts; ++i) {
verts.push_back(_path.verts[i]);
}
}
bool model_path_h::isValid() const { return subsys.isValid(); }
ADE_OBJ(l_ModelPath, model_path_h, "modelpath", "Path of a model");
ADE_FUNC(__len, l_ModelPath, nullptr, "Gets the number of points in this path", "number",
"The number of points or 0 on error")
{
model_path_h* p = nullptr;
if (!ade_get_args(L, "o", l_ModelPath.GetPtr(&p)))
return ade_set_error(L, "s", "");
if (p == nullptr)
return ade_set_error(L, "s", "");
if (!p->isValid())
return ade_set_error(L, "s", "");
return ade_set_args(L, "i", static_cast<int>(p->verts.size()));
}
ADE_INDEXER(l_ModelPath, "number", "Returns the point in the path with the specified index", "modelpathpoint",
"The point or invalid handle if index is invalid")
{
model_path_h* p = nullptr;
int idx;
if (!ade_get_args(L, "oi", l_ModelPath.GetPtr(&p), &idx))
return ade_set_error(L, "o", l_ModelPathPoint.Set(model_path_point_h()));
if (p == nullptr)
return ade_set_error(L, "o", l_ModelPathPoint.Set(model_path_point_h()));
if (!p->isValid())
return ade_set_error(L, "o", l_ModelPathPoint.Set(model_path_point_h()));
idx = idx - 1; // Lua -> FS2
if (idx < 0 || idx >= (int)p->verts.size()) {
return ade_set_error(L, "o", l_ModelPathPoint.Set(model_path_point_h()));
}
auto& vert = p->verts[idx];
return ade_set_args(L, "o", l_ModelPathPoint.Set(model_path_point_h(p, vert.pos, vert.radius)));
}
ADE_VIRTVAR(Name, l_ModelPath, "string", "The name of this model path", "string",
"The name or empty string if handle is invalid")
{
model_path_h* p = nullptr;
if (!ade_get_args(L, "o", l_ModelPath.GetPtr(&p)))
return ade_set_error(L, "s", "");
if (p == nullptr)
return ade_set_error(L, "s", "");
if (!p->isValid())
return ade_set_error(L, "s", "");
return ade_set_args(L, "s", p->name.c_str());
}
} // namespace api
} // namespace scripting