Skip to content

Commit 59e8742

Browse files
committed
remove ship_name_lookup in scripting code
Use the ship registry rather than `ship_name_lookup`. Also, be sure to check that ships, targets, and subsystems exist. Fixes #7109.
1 parent 849a4f6 commit 59e8742

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

code/scripting/api/libs/mission.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -482,22 +482,21 @@ ADE_INDEXER(l_Mission_Ships, "number/string IndexOrName", "Gets ship", "ship", "
482482
if(!ade_get_args(L, "*s", &name))
483483
return ade_set_error(L, "o", l_Ship.Set(object_h()));
484484

485-
int idx = ship_name_lookup(name);
486-
487-
if (idx >= 0)
485+
int objnum = -1;
486+
auto entry = ship_registry_get(name);
487+
if (entry)
488488
{
489-
return ade_set_args(L, "o", l_Ship.Set(object_h(&Objects[Ships[idx].objnum])));
489+
if (entry->has_shipp())
490+
objnum = entry->objnum;
490491
}
491492
else
492493
{
493-
idx = atoi(name);
494-
495-
int objnum = -1;
494+
int idx = atoi(name);
496495
if (idx > 0)
497496
objnum = object_subclass_at_index(Ships, MAX_SHIPS, idx);
498-
499-
return ade_set_args(L, "o", l_Ship.Set(object_h(objnum)));
500497
}
498+
499+
return ade_set_args(L, "o", l_Ship.Set(object_h(objnum)));
501500
}
502501

503502
ADE_FUNC(__len, l_Mission_Ships, NULL,

code/scripting/api/objs/order.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ ADE_FUNC(getType, l_Order, NULL, "Gets the type of the order.", "enumeration", "
188188
// helper function
189189
void maybe_start_waypoints(order_h* ohp, bool force)
190190
{
191+
Assertion(ohp->isValid(), "The order should be valid at this point!");
191192
Assertion(ohp->aigp->wp_list_index >= 0, "This function requires wp_list to be assigned already!");
192193
Assertion(ohp->aigp->target_name && !stricmp(Waypoint_lists[ohp->aigp->wp_list_index].get_name(), ohp->aigp->target_name), "wp_list name does not match target_name!");
193194

@@ -363,10 +364,14 @@ ADE_VIRTVAR(Target, l_Order, "object", "Target of the order. Value may also be a
363364
case AI_GOAL_STAY_STILL:
364365
case AI_GOAL_REARM_REPAIR:
365366
case AI_GOAL_DOCK:
366-
case AI_GOAL_UNDOCK:
367-
shipnum = ship_name_lookup(ohp->aigp->target_name);
368-
objnum = (shipnum >= 0) ? Ships[shipnum].objnum : -1;
367+
case AI_GOAL_UNDOCK: {
368+
auto target_entry = ship_registry_get(ohp->aigp->target_name);
369+
if (target_entry && target_entry->has_shipp()) {
370+
shipnum = target_entry->shipnum;
371+
objnum = target_entry->objnum;
372+
}
369373
break;
374+
}
370375

371376
case AI_GOAL_CHASE_WEAPON:
372377
objnum = Weapons[ohp->aigp->target_instance].objnum;
@@ -413,7 +418,6 @@ ADE_VIRTVAR(TargetSubsystem, l_Order, "subsystem", "Target subsystem of the orde
413418
order_h *ohp = NULL;
414419
ship_subsys_h *newh = NULL;
415420
ai_info *aip = NULL;
416-
object *objp = NULL;
417421
if(!ade_get_args(L, "o|o", l_Order.GetPtr(&ohp), l_Subsystem.GetPtr(&newh)))
418422
return ade_set_error(L, "o", l_Subsystem.Set(ship_subsys_h()));
419423

@@ -426,18 +430,19 @@ ADE_VIRTVAR(TargetSubsystem, l_Order, "subsystem", "Target subsystem of the orde
426430
{
427431
if(newh && newh->isValid() && (ohp->aigp->ai_mode == AI_GOAL_DESTROY_SUBSYSTEM))
428432
{
429-
objp = &Objects[newh->ss->parent_objnum];
430-
if (stricmp(Ships[objp->instance].ship_name, ohp->aigp->target_name)) {
431-
ohp->aigp->target_name = ai_get_goal_target_name(Ships[objp->instance].ship_name, &ohp->aigp->target_name_index);
433+
auto ss_objp = &Objects[newh->ss->parent_objnum];
434+
auto ss_shipp = &Ships[ss_objp->instance];
435+
if (stricmp(ss_shipp->ship_name, ohp->aigp->target_name)) {
436+
ohp->aigp->target_name = ai_get_goal_target_name(ss_shipp->ship_name, &ohp->aigp->target_name_index);
432437
ohp->aigp->time = (ohp->odx == 0) ? Missiontime : 0;
433438
if(ohp->odx == 0) {
434439
aip->ok_to_target_timestamp = timestamp(0);
435-
set_target_objnum(aip, OBJ_INDEX(objp));
440+
set_target_objnum(aip, OBJ_INDEX(ss_objp));
436441
}
437442
}
438-
ohp->aigp->ai_submode = ship_find_subsys( &Ships[objp->instance], newh->ss->system_info->subobj_name );
443+
ohp->aigp->ai_submode = ship_find_subsys( ss_shipp, newh->ss->system_info->subobj_name );
439444
if(ohp->odx == 0) {
440-
set_targeted_subsys(aip, newh->ss, OBJ_INDEX(objp));
445+
set_targeted_subsys(aip, newh->ss, OBJ_INDEX(ss_objp));
441446
}
442447
if (aip == Player_ai) {
443448
Ships[newh->ss->parent_objnum].last_targeted_subobject[Player_num] = newh->ss;
@@ -446,10 +451,12 @@ ADE_VIRTVAR(TargetSubsystem, l_Order, "subsystem", "Target subsystem of the orde
446451
}
447452

448453
if(ohp->aigp->ai_mode == AI_GOAL_DESTROY_SUBSYSTEM){
449-
return ade_set_args(L, "o", l_Subsystem.Set(ship_subsys_h(&Objects[Ships[ship_name_lookup(ohp->aigp->target_name)].objnum], ship_get_indexed_subsys(&Ships[ship_name_lookup(ohp->aigp->target_name)],ohp->aigp->ai_submode))));
450-
} else {
451-
return ade_set_error(L, "o", l_Subsystem.Set(ship_subsys_h()));
454+
auto target_entry = ship_registry_get(ohp->aigp->target_name);
455+
if (target_entry && target_entry->has_shipp() && ohp->aigp->ai_submode >= 0 && ohp->aigp->ai_submode < ship_get_num_subsys(target_entry->shipp())) {
456+
return ade_set_args(L, "o", l_Subsystem.Set(ship_subsys_h(target_entry->objp(), ship_get_indexed_subsys(target_entry->shipp(), ohp->aigp->ai_submode))));
457+
}
452458
}
459+
return ade_set_error(L, "o", l_Subsystem.Set(ship_subsys_h()));
453460
}
454461

455462
ADE_VIRTVAR(WaypointList, l_Order, "waypointlist", "Waypoint list of the order.", "waypointlist", "Waypoint list, or invalid handle if order handle is invalid or if this is not a waypoints order.")

0 commit comments

Comments
 (0)