Files
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

141 lines
4.1 KiB
C++

//
//
#include "waypoint.h"
#include "object.h"
namespace scripting {
namespace api {
//**********HANDLE: Waypoint
ADE_OBJ_DERIV(l_Waypoint, object_h, "waypoint", "waypoint handle", l_Object);
ADE_FUNC(getList, l_Waypoint, NULL, "Returns the waypoint list", "waypointlist", "waypointlist handle or invalid handle if waypoint was invalid")
{
object_h *oh = NULL;
waypoint_list *wp_list = NULL;
if(!ade_get_args(L, "o", l_Waypoint.GetPtr(&oh)))
return ade_set_error(L, "o", l_WaypointList.Set(waypointlist_h()));
if(oh->isValid() && oh->objp()->type == OBJ_WAYPOINT) {
wp_list = find_waypoint_list_with_instance(oh->objp()->instance);
return ade_set_args(L, "o", l_WaypointList.Set(waypointlist_h(wp_list)));
}
return ade_set_error(L, "o", l_WaypointList.Set(waypointlist_h()));
}
ADE_FUNC(getIndex, l_Waypoint, nullptr, "Returns the index of this waypoint in its list", "number", "waypoint index or 0 if waypoint was invalid")
{
object_h *oh = nullptr;
if(!ade_get_args(L, "o", l_Waypoint.GetPtr(&oh)))
return ade_set_error(L, "i", 0);
if(!oh->isValid() || oh->objp()->type != OBJ_WAYPOINT)
return ade_set_error(L, "i", 0);
int wp_index = calc_waypoint_index(oh->objp()->instance);
return ade_set_args(L, "i", wp_index + 1);
}
waypointlist_h::waypointlist_h()
: wl_index(-1)
{}
waypointlist_h::waypointlist_h(int _wl_index)
{
if (SCP_vector_inbounds(Waypoint_lists, _wl_index))
wl_index = _wl_index;
else
wl_index = -1;
}
waypointlist_h::waypointlist_h(waypoint_list* _wlp)
: waypointlist_h((_wlp == nullptr) ? -1 : find_index_of_waypoint_list(_wlp))
{}
waypointlist_h::waypointlist_h(const char* wlname)
: waypointlist_h((wlname == nullptr) ? nullptr : find_matching_waypoint_list(wlname))
{}
bool waypointlist_h::isValid() const
{
return SCP_vector_inbounds(Waypoint_lists, wl_index);
}
waypoint_list* waypointlist_h::getList() const
{
return isValid() ? &Waypoint_lists[wl_index] : nullptr;
}
//**********HANDLE: WaypointList
ADE_OBJ(l_WaypointList, waypointlist_h, "waypointlist", "waypointlist handle");
ADE_INDEXER(l_WaypointList, "number Index", "Array of waypoints that are part of the waypoint list", "waypoint", "Waypoint, or invalid handle if the index or waypointlist handle is invalid")
{
int idx = -1;
waypointlist_h* wlh = nullptr;
if( !ade_get_args(L, "oi", l_WaypointList.GetPtr( &wlh ), &idx))
return ade_set_error( L, "o", l_Waypoint.Set( object_h() ) );
if(!wlh || !wlh->isValid())
return ade_set_error( L, "o", l_Waypoint.Set( object_h() ) );
//Lua-->FS2
idx--;
//Get waypoint
auto wp = find_waypoint_at_index(wlh->getList(), idx);
if (wp)
return ade_set_args(L, "o", l_Waypoint.Set(object_h(wp->get_objnum())));
return ade_set_error(L, "o", l_Waypoint.Set( object_h() ) );
}
ADE_FUNC(__len, l_WaypointList,
NULL,
"Number of waypoints in the list. "
"Note that the value returned cannot be relied on for more than one frame.",
"number",
"Number of waypoints in the list, or 0 if handle is invalid")
{
waypointlist_h* wlh = nullptr;
if ( !ade_get_args(L, "o", l_WaypointList.GetPtr(&wlh)) ) {
return ade_set_error(L, "i", 0);
}
if (!wlh || !wlh->isValid()) {
return ade_set_error(L, "i", 0);
}
return ade_set_args(L, "i", wlh->getList()->get_waypoints().size());
}
ADE_FUNC(__tostring, l_WaypointList, nullptr, "Returns name of waypoint list (if any)", "string", "Waypoint list name, or empty string if handle is invalid")
{
waypointlist_h* wlh = nullptr;
if ( !ade_get_args(L, "o", l_WaypointList.GetPtr(&wlh)) ) {
return ade_set_error(L, "s", "");
}
if (!wlh || !wlh->isValid()) {
return ade_set_error(L, "s", "");
}
return ade_set_args(L, "s", wlh->getList()->get_name());
}
ADE_VIRTVAR(Name, l_WaypointList, "string", "Name of WaypointList", "string", "Waypointlist name, or empty string if handle is invalid")
{
waypointlist_h* wlh = NULL;
const char* s = nullptr;
if ( !ade_get_args(L, "o|s", l_WaypointList.GetPtr(&wlh), &s) ) {
return ade_set_error(L, "s", "");
}
if (!wlh || !wlh->isValid()) {
return ade_set_error(L, "s", "");
}
if(ADE_SETTING_VAR && s != NULL) {
wlh->getList()->set_name(s);
}
return ade_set_args( L, "s", wlh->getList()->get_name());
}
}
}