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

134 lines
2.9 KiB
C++

#include "goal.h"
#include "scripting/ade_args.h"
#include "scripting/ade.h"
#include "scripting/api/objs/team.h"
#include "mission/missiongoals.h"
namespace scripting::api
{
//**********HANDLE: mission goal
ADE_OBJ(l_Goal, int, "mission_goal", "Mission goal handle");
ADE_OBJ_VALIDATOR(l_Goal, idx, Mission_goals.in_bounds(idx));
ADE_VIRTVAR(Name, l_Goal, nullptr, "The name of the goal", "string", "The goal name")
{
int current;
if (!ade_get_args(L, "o", l_Goal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "s", Mission_goals[current].name.c_str());
}
ADE_VIRTVAR(Message, l_Goal, nullptr, "The message of the goal", "string", "The goal message")
{
int current;
if (!ade_get_args(L, "o", l_Goal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "s", Mission_goals[current].message.c_str());
}
ADE_VIRTVAR(Type, l_Goal, nullptr, "The goal type", "string", "primary, secondary, bonus, or none")
{
int current;
if (!ade_get_args(L, "o", l_Goal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
SCP_string type;
int bit = Mission_goals[current].type & GOAL_TYPE_MASK;
switch (bit) {
case PRIMARY_GOAL:
type = "primary";
break;
case SECONDARY_GOAL:
type = "secondary";
break;
case BONUS_GOAL:
type = "bonus";
break;
default:
type = "none";
break;
}
return ade_set_args(L, "s", type);
}
ADE_VIRTVAR(Team, l_Goal, nullptr, "The goal team", "team", "The goal team")
{
int current;
if (!ade_get_args(L, "o", l_Goal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "o", l_Team.Set(Mission_goals[current].team));
}
ADE_VIRTVAR(isGoalSatisfied, l_Goal, nullptr, "The status of the goal", "number", "0 if failed, 1 if complete, 2 if incomplete")
{
int current;
if (!ade_get_args(L, "o", l_Goal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "i", Mission_goals[current].satisfied);
}
ADE_VIRTVAR(Score, l_Goal, nullptr, "The score of the goal", "number", "the score")
{
int current;
if (!ade_get_args(L, "o", l_Goal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "i", Mission_goals[current].score);
}
ADE_VIRTVAR(isGoalValid, l_Goal, nullptr, "The goal validity", "boolean", "true if valid, false otherwise")
{
int current;
if (!ade_get_args(L, "o", l_Goal.Get(&current))) {
return ADE_RETURN_NIL;
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "b", !(Mission_goals[current].type & INVALID_GOAL));
}
}