Files
fs2open.github.com/code/scripting/api/objs/eye.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

91 lines
2.0 KiB
C++

//
//
#include "eye.h"
#include "vecmath.h"
namespace scripting {
namespace api {
eye_h::eye_h() {
model = -1;
eye_idx = -1;
}
eye_h::eye_h(int n_m, int n_e) {
model = n_m;
eye_idx = n_e;
}
bool eye_h::isValid() const {
polymodel* pm = NULL;
return (model > -1
&& (pm = model_get(model)) != NULL
&& eye_idx > -1
&& eye_idx < pm->n_view_positions);
}
ADE_OBJ(l_Eyepoint, eye_h, "eyepoint", "Eyepoint handle");
ADE_VIRTVAR(Normal, l_Eyepoint, "vector", "Eyepoint normal", "vector", "Eyepoint normal, or null vector if handle is invalid")
{
eye_h *eh;
vec3d *v;
if(!ade_get_args(L, "o|o", l_Eyepoint.GetPtr(&eh), l_Vector.GetPtr(&v)))
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(!eh->isValid())
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
polymodel *pm = model_get(eh->model);
if(ADE_SETTING_VAR && v != NULL)
{
pm->view_positions[eh->eye_idx].norm = *v;
}
return ade_set_args(L, "o", l_Vector.Set(pm->view_positions[eh->eye_idx].norm));
}
ADE_VIRTVAR(Position, l_Eyepoint, "vector", "Eyepoint location (Local vector)", "vector", "Eyepoint location, or null vector if handle is invalid")
{
eye_h *eh;
vec3d *v;
if(!ade_get_args(L, "o|o", l_Eyepoint.GetPtr(&eh), l_Vector.GetPtr(&v)))
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(!eh->isValid())
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
polymodel *pm = model_get(eh->model);
if(ADE_SETTING_VAR && v != NULL)
{
pm->view_positions[eh->eye_idx].pnt = *v;
}
return ade_set_args(L, "o", l_Vector.Set(pm->view_positions[eh->eye_idx].pnt));
}
ADE_FUNC_DEPRECATED(IsValid,
l_Eyepoint,
nullptr,
"Detect whether this handle is valid",
"boolean",
"true if valid false otherwise",
gameversion::version(24, 0),
"IsValid is named with the incorrect case. Use isValid instead.")
{
eye_h *eh = nullptr;
if (!ade_get_args(L, "o", l_Eyepoint.GetPtr(&eh)))
{
return ADE_RETURN_FALSE;
}
return ade_set_args(L, "b", eh->isValid());
}
}
}