Skip to content

Commit 3b68971

Browse files
committed
lua mission access to props
1 parent 41f1d18 commit 3b68971

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

code/scripting/api/libs/mission.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "parse/sexp/LuaAISEXP.h"
3737
#include "parse/sexp/sexp_lookup.h"
3838
#include "playerman/player.h"
39+
#include "prop/prop.h"
3940
#include "scripting/api/LuaPromise.h"
4041
#include "scripting/api/objs/LuaEnum.h"
4142
#include "scripting/api/objs/LuaSEXP.h"
@@ -57,6 +58,8 @@
5758
#include "scripting/api/objs/object.h"
5859
#include "scripting/api/objs/parse_object.h"
5960
#include "scripting/api/objs/promise.h"
61+
#include "scripting/api/objs/prop.h"
62+
#include "scripting/api/objs/propclass.h"
6063
#include "scripting/api/objs/sexpvar.h"
6164
#include "scripting/api/objs/ship_registry_entry.h"
6265
#include "scripting/api/objs/ship.h"
@@ -549,6 +552,43 @@ ADE_FUNC(__len, l_Mission_ParsedShips, NULL,
549552
return ade_set_args(L, "i", static_cast<int>(Parse_objects.size()));
550553
}
551554

555+
//****SUBLIBRARY: Mission/Props
556+
ADE_LIB_DERIV(l_Mission_Props, "Props", nullptr, "Props in the mission", l_Mission);
557+
558+
ADE_INDEXER(l_Mission_Props, "number/string IndexOrName", "Gets prop", "prop", "Prop handle, or invalid prop handle if index was invalid")
559+
{
560+
const char* name;
561+
if(!ade_get_args(L, "*s", &name))
562+
return ade_set_error(L, "o", l_Prop.Set(object_h()));
563+
564+
int idx = ship_name_lookup(name);
565+
566+
if (idx >= 0)
567+
{
568+
return ade_set_args(L, "o", l_Prop.Set(object_h(&Objects[Props[idx].objnum])));
569+
}
570+
else
571+
{
572+
idx = atoi(name);
573+
574+
int objnum = -1;
575+
if (idx > 0)
576+
objnum = object_subclass_at_index(Props, MAX_PROPS, idx);
577+
578+
return ade_set_args(L, "o", l_Prop.Set(object_h(objnum)));
579+
}
580+
}
581+
582+
ADE_FUNC(__len, l_Mission_Props, NULL,
583+
"Number of props in the mission. "
584+
"This function is somewhat slow, and should be set to a variable for use in looping situations. "
585+
"Note that props can be vanished, and so this value cannot be relied on for more than one frame.",
586+
"number",
587+
"Number of props in the mission, or 0 if props haven't been initialized yet")
588+
{
589+
return ade_set_args(L, "i", object_subclass_count(Props, MAX_PROPS));
590+
}
591+
552592
//****SUBLIBRARY: Mission/Waypoints
553593
ADE_LIB_DERIV(l_Mission_Waypoints, "Waypoints", NULL, NULL, l_Mission);
554594

@@ -1340,6 +1380,47 @@ ADE_FUNC(createShip,
13401380
return ade_set_error(L, "o", l_Ship.Set(object_h()));
13411381
}
13421382

1383+
ADE_FUNC(createProp,
1384+
l_Mission,
1385+
"[string Name, propclass Class /* First prop class by default */, orientation Orientation=null, vector Position /* null vector by default */]",
1386+
"Creates a prop and returns a handle to it using the specified name, class, world orientation, and world position.",
1387+
"prop",
1388+
"Prop handle, or invalid prop handle if prop couldn't be created")
1389+
{
1390+
const char* name = nullptr;
1391+
int pclass = -1;
1392+
matrix_h* orient = nullptr;
1393+
vec3d pos = vmd_zero_vector;
1394+
ade_get_args(L, "|sooo", &name, l_Propclass.Get(&pclass), l_Matrix.GetPtr(&orient), l_Vector.Get(&pos));
1395+
1396+
if (Prop_info.size() == 0) {
1397+
return ade_set_error(L, "o", l_Prop.Set(object_h()));
1398+
}
1399+
1400+
matrix *real_orient = &vmd_identity_matrix;
1401+
if(orient != NULL)
1402+
{
1403+
real_orient = orient->GetMatrix();
1404+
}
1405+
1406+
if (pclass == -1) {
1407+
return ade_set_error(L, "o", l_Prop.Set(object_h()));
1408+
}
1409+
1410+
int obj_idx = prop_create(real_orient, &pos, pclass, name);
1411+
1412+
if(obj_idx >= 0) {
1413+
auto propp = &Props[Objects[obj_idx].instance];
1414+
1415+
prop_info* pip = &Prop_info[pclass];
1416+
1417+
model_page_in_textures(pip->model_num, pclass);
1418+
1419+
return ade_set_args(L, "o", l_Prop.Set(object_h(&Objects[obj_idx])));
1420+
} else
1421+
return ade_set_error(L, "o", l_Prop.Set(object_h()));
1422+
}
1423+
13431424
ADE_FUNC(createDebris,
13441425
l_Mission,
13451426
"[ship | shipclass | model | submodel | nil source, string | nil submodel_index_or_name, vector position, orientation, enumeration create_flags, "

0 commit comments

Comments
 (0)