@@ -1472,28 +1472,74 @@ ADE_FUNC(createDebris,
14721472 return ade_set_args (L, " o" , l_Debris.Set (object_h (obj)));
14731473}
14741474
1475+ ADE_FUNC (createWaypointList, l_Mission, " [string name]" ,
1476+ " Creates a waypoint list. If a name is not specified, the list will have a default name. Be sure to populate the list with at least one waypoint before using it!" ,
1477+ " waypointlist" ,
1478+ " Waypoint list handle, or invalid handle if waypoint list couldn't be created" )
1479+ {
1480+ char buf[NAME_LENGTH ];
1481+ const char * name = nullptr ;
1482+ if (!ade_get_args (L, " |s" , &name))
1483+ return ade_set_error (L, " o" , l_WaypointList.Set (waypointlist_h ()));
1484+
1485+ // use either a specified name...
1486+ if (name)
1487+ {
1488+ auto list = find_matching_waypoint_list (name);
1489+ if (list)
1490+ {
1491+ Warning (LOCATION , " Waypoint list '%s' already exists!" , name);
1492+ return ade_set_error (L, " o" , l_WaypointList.Set (waypointlist_h ()));
1493+ }
1494+ if (strlen (name) > NAME_LENGTH - 1 )
1495+ {
1496+ Warning (LOCATION , " Waypoint list name '%s' must be at most %d characters!" , name, NAME_LENGTH - 1 );
1497+ return ade_set_error (L, " o" , l_WaypointList.Set (waypointlist_h ()));
1498+ }
1499+ strcpy_s (buf, name);
1500+ }
1501+ // ...or a generated name
1502+ else
1503+ {
1504+ waypoint_find_unique_name (buf, static_cast <int >(Waypoint_lists.size ()) + 1 );
1505+ }
1506+
1507+ // add new list with that name
1508+ Waypoint_lists.emplace_back (buf);
1509+ return ade_set_args (L, " o" , l_WaypointList.Set (waypointlist_h (static_cast <int >(Waypoint_lists.size ()) - 1 )));
1510+ }
1511+
14751512ADE_FUNC (createWaypoint, l_Mission, " [vector Position, waypointlist List]" ,
1476- " Creates a waypoint" ,
1513+ " Creates a waypoint. If Position is not specified, the waypoint will be at (0,0,0). If List is not specified, a new list will be created and the waypoint will be added to it. " ,
14771514 " waypoint" ,
14781515 " Waypoint handle, or invalid waypoint handle if waypoint couldn't be created" )
14791516{
1480- vec3d *v3 = NULL ;
1481- waypointlist_h *wlh = NULL ;
1517+ vec3d *v3 = nullptr ;
1518+ waypointlist_h *wlh = nullptr ;
14821519 if (!ade_get_args (L, " |oo" , l_Vector.GetPtr (&v3), l_WaypointList.GetPtr (&wlh)))
14831520 return ade_set_error (L, " o" , l_Waypoint.Set (object_h ()));
14841521
1485- // determine where we need to create it - it looks like we were given a waypoint list but not a waypoint itself
1522+ // determine where we need to create it - if we were given a waypoint list, put it at the end
1523+ // (unless the list is empty, in which case put it at the beginning)
14861524 int waypoint_instance = -1 ;
1525+ bool first_waypoint_in_list = false ;
14871526 if (wlh && wlh->isValid ())
14881527 {
1489- int wp_list_index = find_index_of_waypoint_list (wlh->getList ());
1490- int wp_index = static_cast <int >(wlh->getList ()->get_waypoints ().size ()) - 1 ;
1528+ int wp_list_index = wlh->wl_index ;
1529+ int wp_index;
1530+ if (wlh->getList ()->get_waypoints ().empty ())
1531+ {
1532+ wp_index = 0 ;
1533+ first_waypoint_in_list = true ;
1534+ }
1535+ else
1536+ wp_index = static_cast <int >(wlh->getList ()->get_waypoints ().size ()) - 1 ;
14911537 waypoint_instance = calc_waypoint_instance (wp_list_index, wp_index);
14921538 }
1493- int obj_idx = waypoint_add (v3 != NULL ? v3 : &vmd_zero_vector, waypoint_instance);
1539+ int obj_idx = waypoint_add (v3 != nullptr ? v3 : &vmd_zero_vector, waypoint_instance, first_waypoint_in_list );
14941540
14951541 if (obj_idx >= 0 )
1496- return ade_set_args (L, " o" , l_Waypoint.Set (object_h (&Objects[ obj_idx] )));
1542+ return ade_set_args (L, " o" , l_Waypoint.Set (object_h (obj_idx)));
14971543 else
14981544 return ade_set_args (L, " o" , l_Waypoint.Set (object_h ()));
14991545}
0 commit comments