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

149 lines
3.3 KiB
C++

#include "medals.h"
#include "stats/medals.h"
namespace scripting {
namespace api {
medal_h::medal_h() : medal(-1) {}
medal_h::medal_h(int l_medal) : medal(l_medal) {}
medal_stuff* medal_h::getMedal() const
{
if (!isValid())
return nullptr;
return &Medals[medal];
}
bool medal_h::isValid() const
{
return SCP_vector_inbounds(Medals, medal);
}
bool medal_h::isRank() const
{
return medal == Rank_medal_index;
}
//**********HANDLE: medal
ADE_OBJ(l_Medal, medal_h, "medal", "Medal handle");
ADE_VIRTVAR(Name, l_Medal, nullptr, "The name of the medal", "string", "The name")
{
medal_h current;
if (!ade_get_args(L, "o", l_Medal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ade_set_error(L, "s", "");
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "s", current.getMedal()->name);
}
ADE_VIRTVAR(DisplayName, l_Medal, nullptr, "The display name of the medal", "string", "The display name")
{
medal_h current;
if (!ade_get_args(L, "o", l_Medal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ade_set_error(L, "s", "");
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "s", current.getMedal()->get_display_name());
}
ADE_VIRTVAR(Bitmap, l_Medal, nullptr, "The bitmap of the medal", "string", "The bitmap")
{
medal_h current;
if (!ade_get_args(L, "o", l_Medal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ade_set_error(L, "s", "");
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "s", current.getMedal()->bitmap);
}
ADE_VIRTVAR(NumMods, l_Medal, nullptr, "The number of mods of the medal", "number", "The number of mods")
{
medal_h current;
if (!ade_get_args(L, "o", l_Medal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ade_set_error(L, "i", 0);
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "i", current.getMedal()->num_versions);
}
ADE_VIRTVAR(FirstMod, l_Medal, nullptr, "The first mod of the medal. Some start at 1, some start at 0", "number", "The first mod")
{
medal_h current;
if (!ade_get_args(L, "o", l_Medal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ade_set_error(L, "i", 0);
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
int start = 0;
if (current.getMedal()->version_starts_at_1)
start = 1;
return ade_set_args(L, "i", start);
}
ADE_VIRTVAR(KillsNeeded, l_Medal, nullptr, "The number of kills needed to earn this badge. If not a badge, then returns 0", "number", "The number of kills needed")
{
medal_h current;
if (!ade_get_args(L, "o", l_Medal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ade_set_error(L, "i", 0);
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "i", current.getMedal()->kills_needed);
}
ADE_FUNC(isRank, l_Medal, nullptr, "Detects whether medal is the rank medal", "boolean", "true if yes, false if not, nil if a syntax/type error occurs")
{
medal_h current;
if (!ade_get_args(L, "o", l_Medal.Get(&current)))
return ADE_RETURN_NIL;
return ade_set_args(L, "b", current.isRank());
}
} // namespace api
} // namespace scripting