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

139 lines
3.5 KiB
C++

#include "shields.h"
#include "enums.h"
#include "object/objectshield.h"
namespace scripting {
namespace api {
//**********HANDLE: Shields
ADE_OBJ(l_Shields, object_h, "shields", "Shields handle");
ADE_FUNC(__len, l_Shields, nullptr, "Number of shield segments", "number", "Number of shield segments or -1 if handle is invalid")
{
object_h *objh;
if(!ade_get_args(L, "o", l_Shields.GetPtr(&objh)))
return ade_set_error(L, "i", -1);
if(!objh->isValid())
return ade_set_error(L, "i", -1);
return ade_set_args(L, "i", static_cast<int>(objh->objp()->shield_quadrant.size()));
}
ADE_INDEXER(l_Shields, "enumeration/number", "Gets or sets shield segment strength. Use \"SHIELD_*\" enumerations (for standard 4-quadrant shields) or index of a specific segment, or SHIELD_NONE for the entire shield", "number", "Segment/shield strength, or 0 if handle is invalid")
{
object_h *objh;
float nval = -1.0f;
object *objp = NULL;
int qdx = -1;
if(lua_isstring(L, 2))
{
const char* qd = nullptr;
if(!ade_get_args(L, "os|f", l_Shields.GetPtr(&objh), &qd, &nval))
return ade_set_error(L, "f", 0.0f);
if(!objh->isValid())
return ade_set_error(L, "f", 0.0f);
objp = objh->objp();
//Which quadrant?
int qdi;
if(qd == NULL)
qdx = -1;
else if((qdi = atoi(qd)) > 0 && qdi <= static_cast<int>(objp->shield_quadrant.size()))
qdx = qdi-1; //LUA->FS2
else
return ade_set_error(L, "f", 0.0f);
} else {
enum_h *qd = NULL;
if(!ade_get_args(L, "oo|f", l_Shields.GetPtr(&objh), l_Enum.GetPtr(&qd), &nval))
return 0;
if(!objh->isValid())
return ade_set_error(L, "f", 0.0f);
objp = objh->objp();
switch(qd->index)
{
case LE_NONE:
qdx = -1;
break;
case LE_SHIELD_FRONT:
qdx = FRONT_QUAD;
break;
case LE_SHIELD_LEFT:
qdx = LEFT_QUAD;
break;
case LE_SHIELD_RIGHT:
qdx = RIGHT_QUAD;
break;
case LE_SHIELD_BACK:
qdx = REAR_QUAD;
break;
default:
return ade_set_error(L, "f", 0.0f);
}
}
//Set/get all quadrants
if(qdx == -1) {
if(ADE_SETTING_VAR && nval >= 0.0f)
shield_set_strength(objp, nval);
return ade_set_args(L, "f", shield_get_strength(objp));
}
//Set one quadrant?
if(ADE_SETTING_VAR && nval >= 0.0f)
shield_set_quad(objp, qdx, nval);
//Get one quadrant
return ade_set_args(L, "f", shield_get_quad(objp, qdx));
}
//WMC - Not sure if I want this to be a variable. It'd make more sense
//as a function, since it modifies all quadrant variables
//WMC - Ehh, screw it.
ADE_VIRTVAR(CombinedLeft, l_Shields, "number", "Total shield hitpoints left (for all segments combined)", "number", "Combined shield strength, or 0 if handle is invalid")
{
object_h *objh;
float nval = -1.0f;
if(!ade_get_args(L, "o|f", l_Shields.GetPtr(&objh), &nval))
return ade_set_error(L, "f", 0.0f);
if(!objh->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR && nval >= 0.0f) {
shield_set_strength(objh->objp(), nval);
}
return ade_set_args(L, "f", shield_get_strength(objh->objp()));
}
ADE_VIRTVAR(CombinedMax, l_Shields, "number", "Maximum shield hitpoints (for all segments combined)", "number", "Combined maximum shield strength, or 0 if handle is invalid")
{
object_h *objh;
float nval = -1.0f;
if(!ade_get_args(L, "o|f", l_Shields.GetPtr(&objh), &nval))
return 0;
if(!objh->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR && nval >= 0.0f) {
shield_set_max_strength(objh->objp(), nval);
}
return ade_set_args(L, "f", shield_get_max_strength(objh->objp()));
}
}
}