Files
fs2open.github.com/code/scripting/api/objs/support_rearm_pool.cpp
T
Goober5000andClaude Opus 4.7 a168f5a9c0 Store the support rearm pool sparsely (#7758)
`support_ship_info::rearm_weapon_pool` was a dense per-weapon-class array where -1 = unlimited (the default), 0 = not rearmable, and >0 = remaining stockpile.  Convert it to a sparse per-team map:

  `std::array<SCP_map<int, int>, MAX_TVT_TEAMS> rearm_weapon_pool;`
  `int rearm_pool_default() const;  // -1, or 0 under rearm_pool_from_loadout`

Mission-file format and save output are unchanged.  Also tightens the two weapon-class bounds checks in ship.cpp.  This removes another `MAX_WEAPON_TYPES` sized structure on the way to lifting the weapon class limit.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
2026-09-08 10:50:22 -04:00

85 lines
2.2 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_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