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

118 lines
3.5 KiB
C++

//
//
#include "fireballclass.h"
#include "model.h"
#include "fireball/fireballs.h"
namespace scripting {
namespace api {
//**********HANDLE: Fireballclass
ADE_OBJ(l_Fireballclass, int, "fireballclass", "Fireball class handle");
ADE_OBJ_VALIDATOR(l_Fireballclass, idx, Fireball_info.in_bounds(idx));
ADE_FUNC(__tostring, l_Fireballclass, NULL, "Fireball class name", "string", "Fireball class unique id, or an empty string if handle is invalid")
{
int idx;
if(!ade_get_args(L, "o", l_Fireballclass.Get(&idx)))
return ade_set_error(L, "s", "");
if (!SCP_vector_inbounds(Fireball_info, idx))
return ade_set_error(L, "s", "");
return ade_set_args(L, "s", Fireball_info[idx].unique_id);
}
ADE_FUNC(__eq, l_Fireballclass, "fireballclass, fireballclass", "Checks if the two classes are equal", "boolean", "true if equal, false otherwise")
{
int idx1,idx2;
if(!ade_get_args(L, "oo", l_Fireballclass.Get(&idx1), l_Fireballclass.Get(&idx2)))
return ade_set_error(L, "b", false);
if (!SCP_vector_inbounds(Fireball_info, idx1))
return ade_set_error(L, "b", false);
if (!SCP_vector_inbounds(Fireball_info, idx2))
return ade_set_error(L, "b", false);
return ade_set_args(L, "b", idx1 == idx2);
}
ADE_VIRTVAR(UniqueID, l_Fireballclass, "string", "Fireball class name", "string", "Fireball class unique id, or empty string if handle is invalid")
{
int idx;
if(!ade_get_args(L, "o", l_Fireballclass.Get(&idx)))
return ade_set_error(L, "s", "");
if (!SCP_vector_inbounds(Fireball_info, idx))
return ade_set_error(L, "s", "");
if (ADE_SETTING_VAR)
LuaError(L, "This property is read-only");
return ade_set_args(L, "s", Fireball_info[idx].unique_id);
}
ADE_VIRTVAR(Filename, l_Fireballclass, NULL, "Fireball class animation filename (LOD 0)", "string", "Filename, or empty string if handle is invalid")
{
int idx;
if(!ade_get_args(L, "o", l_Fireballclass.Get(&idx)))
return ade_set_error(L, "s", "");
if (!SCP_vector_inbounds(Fireball_info, idx))
return ade_set_error(L, "s", "");
//Currently not settable as the bitmaps are only loaded once at level start
if (ADE_SETTING_VAR)
LuaError(L, "This property is read-only");
return ade_set_args(L, "s", Fireball_info[idx].lod[0].filename);
}
ADE_VIRTVAR(NumberFrames, l_Fireballclass, NULL, "Amount of frames the animation has (LOD 0)", "number", "Amount of frames, or -1 if handle is invalid")
{
int idx;
if(!ade_get_args(L, "o", l_Fireballclass.Get(&idx)))
return ade_set_error(L, "i", -1);
if (!SCP_vector_inbounds(Fireball_info, idx))
return ade_set_error(L, "i", -1);
if (ADE_SETTING_VAR)
LuaError(L, "This property is read-only");
return ade_set_args(L, "i", Fireball_info[idx].lod[0].num_frames);
}
ADE_VIRTVAR(FPS, l_Fireballclass, NULL, "The FPS with which this fireball's animation is played (LOD 0)", "number", "FPS, or -1 if handle is invalid")
{
int idx;
if (!ade_get_args(L, "o", l_Fireballclass.Get(&idx)))
return ade_set_error(L, "i", -1);
if (!SCP_vector_inbounds(Fireball_info, idx))
return ade_set_error(L, "i", -1);
if (ADE_SETTING_VAR)
LuaError(L, "This property is read-only");
return ade_set_args(L, "i", Fireball_info[idx].lod[0].fps);
}
ADE_FUNC(getTableIndex, l_Fireballclass, NULL, "Gets the index value of the fireball class", "number", "index value of the fireball class")
{
int idx;
if(!ade_get_args(L, "o", l_Fireballclass.Get(&idx)))
return ade_set_args(L, "i", -1);
if (!SCP_vector_inbounds(Fireball_info, idx))
return ade_set_args(L, "i", -1);
return ade_set_args(L, "i", idx + 1);
}
}
}