mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-09 04:49:39 -10:00
1) Make Intel_info a vector instead of a fixed-size array 2) Make intel descriptions SCP_string so they can be any length
145 lines
3.7 KiB
C++
145 lines
3.7 KiB
C++
//
|
|
//
|
|
|
|
#include "tables.h"
|
|
|
|
#include "scripting/api/objs/shipclass.h"
|
|
#include "scripting/api/objs/weaponclass.h"
|
|
#include "scripting/api/objs/intelentry.h"
|
|
|
|
#include "ship/ship.h"
|
|
#include "weapon/weapon.h"
|
|
#include "menuui/techmenu.h"
|
|
|
|
|
|
extern bool Ships_inited;
|
|
extern bool Weapons_inited;
|
|
extern bool Intel_inited;
|
|
|
|
|
|
namespace scripting {
|
|
namespace api {
|
|
|
|
//**********LIBRARY: Tables
|
|
ADE_LIB(l_Tables, "Tables", "tb", "Tables library");
|
|
|
|
//*****SUBLIBRARY: Tables/ShipClasses
|
|
ADE_LIB_DERIV(l_Tables_ShipClasses, "ShipClasses", NULL, NULL, l_Tables);
|
|
ADE_INDEXER(l_Tables_ShipClasses, "number/string IndexOrName", "Array of ship classes", "shipclass", "Ship class handle, or invalid handle if index is invalid")
|
|
{
|
|
if(!Ships_inited)
|
|
return ade_set_error(L, "o", l_Shipclass.Set(-1));
|
|
|
|
const char* name;
|
|
if(!ade_get_args(L, "*s", &name))
|
|
return ade_set_error(L, "o", l_Shipclass.Set(-1));
|
|
|
|
int idx = ship_info_lookup(name);
|
|
|
|
if(idx < 0) {
|
|
try {
|
|
idx = std::stoi(name);
|
|
idx--; // Lua->FS2
|
|
} catch (const std::exception&) {
|
|
// Not a number
|
|
return ade_set_error(L, "o", l_Shipclass.Set(-1));
|
|
}
|
|
|
|
if (idx < 0 || idx >= ship_info_size()) {
|
|
return ade_set_error(L, "o", l_Shipclass.Set(-1));
|
|
}
|
|
}
|
|
|
|
return ade_set_args(L, "o", l_Shipclass.Set(idx));
|
|
}
|
|
|
|
ADE_FUNC(__len, l_Tables_ShipClasses, NULL, "Number of ship classes", "number", "Number of ship classes, or 0 if ship classes haven't been loaded yet")
|
|
{
|
|
if(!Ships_inited)
|
|
return ade_set_args(L, "i", 0); //No ships loaded...should be 0
|
|
|
|
return ade_set_args(L, "i", Ship_info.size());
|
|
}
|
|
|
|
//*****SUBLIBRARY: Tables/WeaponClasses
|
|
ADE_LIB_DERIV(l_Tables_WeaponClasses, "WeaponClasses", NULL, NULL, l_Tables);
|
|
|
|
ADE_INDEXER(l_Tables_WeaponClasses, "number/string IndexOrWeaponName", "Array of weapon classes", "weapon", "Weapon class handle, or invalid handle if index is invalid")
|
|
{
|
|
if(!Weapons_inited)
|
|
return ade_set_error(L, "o", l_Weaponclass.Set(-1));
|
|
|
|
const char* name;
|
|
if(!ade_get_args(L, "*s", &name))
|
|
return 0;
|
|
|
|
int idx = weapon_info_lookup(name);
|
|
|
|
if(idx < 0) {
|
|
idx = atoi(name);
|
|
|
|
// atoi is good enough here, 0 is invalid anyway
|
|
if (idx > 0)
|
|
{
|
|
idx--; // Lua --> C/C++
|
|
}
|
|
else
|
|
{
|
|
return ade_set_args(L, "o", l_Weaponclass.Set(-1));
|
|
}
|
|
}
|
|
|
|
return ade_set_args(L, "o", l_Weaponclass.Set(idx));
|
|
}
|
|
|
|
ADE_FUNC(__len, l_Tables_WeaponClasses, NULL, "Number of weapon classes", "number", "Number of weapon classes, or 0 if weapon classes haven't been loaded yet")
|
|
{
|
|
if(!Weapons_inited)
|
|
return ade_set_args(L, "i", 0);
|
|
|
|
return ade_set_args(L, "i", weapon_info_size());
|
|
}
|
|
|
|
//*****SUBLIBRARY: Tables/IntelEntries
|
|
ADE_LIB_DERIV(l_Tables_IntelEntries, "IntelEntries", nullptr, nullptr, l_Tables);
|
|
ADE_INDEXER(l_Tables_IntelEntries, "number/string IndexOrName", "Array of intel entries", "intel_entry", "Intel entry handle, or invalid handle if index is invalid")
|
|
{
|
|
if(!Intel_inited)
|
|
return ade_set_error(L, "o", l_Intelentry.Set(-1));
|
|
|
|
const char* name;
|
|
if(!ade_get_args(L, "*s", &name))
|
|
return ade_set_error(L, "o", l_Intelentry.Set(-1));
|
|
|
|
int idx = intel_info_lookup(name);
|
|
|
|
if(idx < 0) {
|
|
try {
|
|
idx = std::stoi(name);
|
|
idx--; // Lua->FS2
|
|
} catch (const std::exception&) {
|
|
// Not a number
|
|
return ade_set_error(L, "o", l_Intelentry.Set(-1));
|
|
}
|
|
|
|
if (idx < 0 || idx >= intel_info_size()) {
|
|
return ade_set_error(L, "o", l_Intelentry.Set(-1));
|
|
}
|
|
}
|
|
|
|
return ade_set_args(L, "o", l_Intelentry.Set(idx));
|
|
}
|
|
|
|
ADE_FUNC(__len, l_Tables_IntelEntries, nullptr, "Number of intel entries", "number", "Number of intel entries, or 0 if intel entries haven't been loaded yet")
|
|
{
|
|
if(!Intel_inited)
|
|
return ade_set_args(L, "i", 0); //No intel loaded...should be 0
|
|
|
|
return ade_set_args(L, "i", intel_info_size());
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|