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

86 lines
2.3 KiB
C++

//
//
#include "support_rearm_pool.h"
#include "mission/missionparse.h"
#include "scripting/api/objs/weaponclass.h"
#include "weapon/weapon.h"
namespace scripting::api {
//****HANDLE: support_rearm_pool_team
ADE_OBJ(l_SupportRearmPoolTeam, int, "support_rearm_pool_team", "Support rearm pool team handle");
ADE_OBJ_VALIDATOR(l_SupportRearmPoolTeam, idx, idx >= 0 && idx < Num_teams && idx < MAX_TVT_TEAMS);
ADE_INDEXER(l_SupportRearmPoolTeam,
"number/weaponclass IndexOrClass, number amount",
"Gets/sets support rearm pool value for a weapon class on this team.\n"
"Values: -1 = unlimited, 0 = unavailable, >0 = limited amount.\n"
"If a weapon has $Disallow Support Rearm, this always returns 0 and ignores writes.",
"number",
"Current pool value for the weapon class.")
{
int team_idx = -1;
int idx = -1;
int amount;
if (lua_isnumber(L, 2)) {
if (!ade_get_args(L, "oi|i", l_SupportRearmPoolTeam.Get(&team_idx), &idx, &amount)) {
return ADE_RETURN_NIL;
}
if (idx < 1 || idx > weapon_info_size()) {
return ADE_RETURN_NIL;
}
idx--; // Lua to C++ index
} else {
if (!ade_get_args(L, "oo|i", l_SupportRearmPoolTeam.Get(&team_idx), l_Weaponclass.Get(&idx), &amount)) {
return ADE_RETURN_NIL;
}
if (idx < 0 || idx >= weapon_info_size()) {
return ADE_RETURN_NIL;
}
}
if (team_idx < 0 || team_idx >= Num_teams || team_idx >= MAX_TVT_TEAMS) {
return ADE_RETURN_NIL;
}
auto &team_pool = The_mission.support_ships.rearm_weapon_pool[team_idx];
int pool_default = The_mission.support_ships.rearm_pool_default();
if (ADE_SETTING_VAR) {
// disallow_rearm weapons always read as 0, so there is nothing to store for them
if (!Weapon_info[idx].disallow_rearm) {
int new_value = (amount < 0) ? -1 : amount;
// don't store the default value; absence means the default
if (new_value == pool_default) {
team_pool.erase(idx);
} else {
team_pool[idx] = new_value;
}
}
}
if (Weapon_info[idx].disallow_rearm) {
return ade_set_args(L, "i", 0);
}
return ade_set_args(L, "i", team_pool.value_or(idx, pool_default));
}
ADE_FUNC(__len,
l_SupportRearmPoolTeam,
nullptr,
"The number of weapon classes in this support rearm pool.",
"number",
"The number of weapon classes.")
{
return ade_set_args(L, "i", weapon_info_size());
}
} // namespace scripting::api