mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-07 08:15:59 -10:00
* 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]>
100 lines
3.6 KiB
C++
100 lines
3.6 KiB
C++
//
|
|
//
|
|
|
|
#include "decaldefinition.h"
|
|
#include "scripting/api/objs/object.h"
|
|
#include "scripting/api/objs/model.h"
|
|
#include "scripting/api/objs/vecmath.h"
|
|
#include "decals/decals.h"
|
|
|
|
namespace scripting {
|
|
namespace api {
|
|
|
|
|
|
//**********HANDLE: DecalDefinitionclass
|
|
ADE_OBJ(l_DecalDefinitionclass, int, "decaldefinition", "Decal definition handle");
|
|
ADE_OBJ_VALIDATOR(l_DecalDefinitionclass, idx, decals::DecalDefinitions.in_bounds(idx));
|
|
|
|
ADE_FUNC(__tostring, l_DecalDefinitionclass, nullptr, "Decal definition name", "string", "Decal definition unique id, or an empty string if handle is invalid")
|
|
{
|
|
int idx;
|
|
const char* s = nullptr;
|
|
if (!ade_get_args(L, "o|s", l_DecalDefinitionclass.Get(&idx), &s))
|
|
return ade_set_error(L, "s", "");
|
|
|
|
if (idx < 0 || idx >= static_cast<int>(decals::DecalDefinitions.size()))
|
|
return ade_set_error(L, "s", "");
|
|
|
|
return ade_set_args(L, "s", decals::DecalDefinitions[idx].getName());
|
|
}
|
|
|
|
ADE_FUNC(__eq, l_DecalDefinitionclass, "decaldefinition, decaldefinition", "Checks if the two definitions are equal", "boolean", "true if equal, false otherwise")
|
|
{
|
|
int idx1,idx2;
|
|
if (!ade_get_args(L, "oo", l_DecalDefinitionclass.Get(&idx1), l_DecalDefinitionclass.Get(&idx2)))
|
|
return ade_set_error(L, "b", false);
|
|
|
|
if (idx1 < 0 || idx1 >= static_cast<int>(decals::DecalDefinitions.size()))
|
|
return ade_set_error(L, "b", false);
|
|
|
|
if (idx2 < 0 || idx2 >= static_cast<int>(decals::DecalDefinitions.size()))
|
|
return ade_set_error(L, "b", false);
|
|
|
|
return ade_set_args(L, "b", idx1 == idx2);
|
|
}
|
|
|
|
ADE_VIRTVAR(Name, l_DecalDefinitionclass, "string", "Decal definition name", "string", "Decal definition name, or empty string if handle is invalid")
|
|
{
|
|
int idx;
|
|
const char* s = nullptr;
|
|
if (!ade_get_args(L, "o|s", l_DecalDefinitionclass.Get(&idx), &s))
|
|
return ade_set_error(L, "s", "");
|
|
|
|
if (idx < 0 || idx >= static_cast<int>(decals::DecalDefinitions.size()))
|
|
return ade_set_error(L, "s", "");
|
|
|
|
if (ADE_SETTING_VAR)
|
|
LuaError(L, "Setting the decal definition name is not implemented");
|
|
|
|
return ade_set_args(L, "s", decals::DecalDefinitions[idx].getName());
|
|
}
|
|
|
|
ADE_FUNC(create, l_DecalDefinitionclass, "number width, number height, number minLifetime, number maxLifetime, object host, submodel submodel, [vector local_pos, orientation local_orient]",
|
|
"Creates a decal with the specified parameters. A negative value for either lifetime will result in a perpetual decal. The position and orientation are in the frame-of-reference of the submodel.", nullptr, "Nothing")
|
|
{
|
|
int idx;
|
|
float width, height, minLifetime, maxLifetime;
|
|
object_h *objh = nullptr;
|
|
submodel_h *smh = nullptr;
|
|
vec3d *local_pos = nullptr;
|
|
matrix_h *local_orient = nullptr;
|
|
|
|
if (!ade_get_args(L, "offffoo|oo", l_DecalDefinitionclass.Get(&idx), &width, &height, &minLifetime, &maxLifetime, l_Object.GetPtr(&objh), l_Submodel.GetPtr(&smh), l_Vector.GetPtr(&local_pos), l_Matrix.GetPtr(&local_orient)))
|
|
return ADE_RETURN_NIL;
|
|
|
|
if (idx < 0 || idx >= static_cast<int>(decals::DecalDefinitions.size()))
|
|
return ADE_RETURN_NIL;
|
|
|
|
if (!objh->isValid() || !smh->isValid())
|
|
return ADE_RETURN_NIL;
|
|
|
|
decals::creation_info info;
|
|
info.definition_handle = idx;
|
|
info.width = width;
|
|
info.height = height;
|
|
|
|
if (minLifetime <= 0.0f || maxLifetime <= 0.0f)
|
|
info.lifetime = util::UniformFloatRange(-1.0f);
|
|
else if (minLifetime == maxLifetime)
|
|
info.lifetime = util::UniformFloatRange(minLifetime);
|
|
else
|
|
info.lifetime = util::UniformFloatRange(minLifetime, maxLifetime);
|
|
|
|
decals::addDecal(info, objh->objp(), smh->GetSubmodelIndex(), local_pos == nullptr ? vmd_zero_vector : *local_pos, local_orient == nullptr ? vmd_identity_matrix : *local_orient->GetMatrix());
|
|
|
|
return ADE_RETURN_NIL;
|
|
}
|
|
|
|
}
|
|
}
|