Skip to content

Commit c4efdfb

Browse files
authored
Merge pull request #7104 from Goober5000/waypoint_stuff_name
add waypoint_stuff_name
2 parents 239e027 + 1dc68b2 commit c4efdfb

15 files changed

Lines changed: 105 additions & 72 deletions

code/object/waypoint.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,49 @@ void waypoint_find_unique_name(char *dest_name, int start_index)
393393
} while (collision != NULL);
394394
}
395395

396+
void waypoint_stuff_name(char *dest, const char *waypoint_list_name, int waypoint_num)
397+
{
398+
constexpr size_t name_max_len = NAME_LENGTH - 3 - 1 - 1; // a colon, three digits, and a null terminator
399+
400+
if (waypoint_num < 1)
401+
{
402+
Assertion(LOCATION, "A waypoint number must be at least 1!");
403+
*dest = 0;
404+
return;
405+
}
406+
if (waypoint_num >= 1000)
407+
{
408+
Error(LOCATION, "This waypoint number has more than three digits! If you actually need this, first convince a coder that you are sane and then ask for the limit to be increased.");
409+
*dest = 0;
410+
return;
411+
}
412+
413+
strncpy(dest, waypoint_list_name, name_max_len);
414+
sprintf(dest + name_max_len, ":%d", waypoint_num);
415+
}
416+
417+
void waypoint_stuff_name(SCP_string &dest, const char *waypoint_list_name, int waypoint_num)
418+
{
419+
constexpr size_t name_max_len = NAME_LENGTH - 3 - 1 - 1; // a colon, three digits, and a null terminator
420+
421+
if (waypoint_num < 1)
422+
{
423+
Assertion(LOCATION, "A waypoint number must be at least 1!");
424+
dest = "";
425+
return;
426+
}
427+
if (waypoint_num >= 1000)
428+
{
429+
Error(LOCATION, "This waypoint number has more than three digits! If you actually need this, first convince a coder that you are sane and then ask for the limit to be increased.");
430+
dest = "";
431+
return;
432+
}
433+
434+
dest.assign(waypoint_list_name, name_max_len);
435+
dest += ":";
436+
dest.append(std::to_string(waypoint_num));
437+
}
438+
396439
void waypoint_add_list(const char *name, const SCP_vector<vec3d> &vec_list)
397440
{
398441
Assert(name != NULL);

code/object/waypoint.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ int find_index_of_waypoint(const waypoint_list *wp_list, const waypoint *wpt);
9292
// Find a name that doesn't conflict with any current waypoint list
9393
void waypoint_find_unique_name(char *dest_name, int start_index);
9494

95+
// Write a waypoint name to a string buffer. Note that waypoint_num is written verbatim, i.e. not adding or subtracting 1. The buffer size must be at least NAME_LENGTH.
96+
void waypoint_stuff_name(char *dest, const char *waypoint_list_name, int waypoint_num);
97+
98+
// Write a waypoint name to a string buffer. Note that waypoint_num is written verbatim, i.e. not adding or subtracting 1.
99+
void waypoint_stuff_name(SCP_string &dest, const char *waypoint_list_name, int waypoint_num);
100+
101+
template <typename STR>
102+
void waypoint_stuff_name(STR &dest, const waypoint &wpt)
103+
{
104+
waypoint_stuff_name(dest, wpt.get_parent_list()->get_name(), wpt.get_index() + 1);
105+
}
106+
107+
template <typename STR>
108+
void waypoint_stuff_name(STR &dest, int waypoint_instance)
109+
{
110+
int wl_index, wp_index;
111+
calc_waypoint_indexes(waypoint_instance, wl_index, wp_index);
112+
Assertion(wl_index >= 0, "Waypoint list must exist!");
113+
Assertion(Waypoint_lists.in_bounds(wp_index), "Waypoint index must be in bounds!");
114+
waypoint_stuff_name(dest, Waypoint_lists[wl_index].get_name(), wp_index + 1);
115+
}
116+
95117
// Add a new list of waypoints. Called from mission parsing.
96118
void waypoint_add_list(const char *name, const SCP_vector<vec3d> &vec_list);
97119

fred2/calcrelativecoordsdlg.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,11 @@ BOOL calc_relative_coords_dlg::OnInitDialog()
6565
}
6666
else if (ptr->type == OBJ_WAYPOINT)
6767
{
68-
SCP_string text;
69-
int waypoint_num;
70-
71-
auto wp_list = find_waypoint_list_with_instance(ptr->instance, &waypoint_num);
72-
Assert(wp_list != nullptr);
73-
sprintf(text, "%s:%d", wp_list->get_name(), waypoint_num + 1);
74-
m_origin_list.AddString(text.c_str());
75-
m_satellite_list.AddString(text.c_str());
68+
char text[NAME_LENGTH];
69+
waypoint_stuff_name(text, ptr->instance);
70+
71+
m_origin_list.AddString(text);
72+
m_satellite_list.AddString(text);
7673

7774
added = true;
7875
}

fred2/fredview.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,12 +1489,11 @@ void CFREDView::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
14891489
str.Format("Edit %s", jnp->GetName());
14901490

14911491
} else if (Objects[objnum].type == OBJ_WAYPOINT) {
1492-
int idx;
1493-
waypoint_list *wp_list = find_waypoint_list_with_instance(Objects[objnum].instance, &idx);
1494-
Assert(wp_list != NULL);
1492+
char text[NAME_LENGTH];
1493+
waypoint_stuff_name(text, Objects[objnum].instance);
14951494

14961495
id = ID_EDITORS_WAYPOINT;
1497-
str.Format("Edit %s:%d", wp_list->get_name(), idx + 1);
1496+
str.Format("Edit %s", text);
14981497

14991498
} else if (Objects[objnum].type == OBJ_POINT) {
15001499
return;
@@ -2604,7 +2603,7 @@ int CFREDView::global_error_check()
26042603
return internal_error("Object references an illegal waypoint number in path");
26052604
}
26062605

2607-
sprintf(buf, "%s:%d", wp_list->get_name(), waypoint_num + 1);
2606+
waypoint_stuff_name(buf, i);
26082607
names[obj_count] = new char[strlen(buf) + 1];
26092608
strcpy(names[obj_count], buf);
26102609
flags[obj_count] = 1;
@@ -2930,8 +2929,8 @@ int CFREDView::global_error_check()
29302929
}
29312930
}
29322931

2933-
for (j = 0; (uint) j < ii.get_waypoints().size(); j++) {
2934-
sprintf(buf, "%s:%d", ii.get_name(), j + 1);
2932+
for (const auto &jj: ii.get_waypoints()) {
2933+
waypoint_stuff_name(buf, jj);
29352934
for (z=0; z<obj_count; z++){
29362935
if (names[z]){
29372936
if (!stricmp(names[z], buf)){

fred2/management.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,7 @@ int common_object_delete(int obj)
12811281
waypoint *wpt = find_waypoint_with_instance(Objects[obj].instance);
12821282
Assert(wpt != NULL);
12831283
waypoint_list *wp_list = wpt->get_parent_list();
1284-
int index = calc_waypoint_list_index(Objects[obj].instance);
1285-
int count = (int) wp_list->get_waypoints().size();
1284+
auto count = wp_list->get_waypoints().size();
12861285

12871286
// we'll end up deleting the path, so check for path references
12881287
if (count == 1) {
@@ -1293,7 +1292,7 @@ int common_object_delete(int obj)
12931292
}
12941293

12951294
// check for waypoint references
1296-
sprintf(msg, "%s:%d", wp_list->get_name(), index + 1);
1295+
waypoint_stuff_name(msg, *wpt);
12971296
name = msg;
12981297
r = reference_handler(name, sexp_ref_type::WAYPOINT, obj);
12991298
if (r)
@@ -2306,8 +2305,6 @@ int sexp_reference_handler(int node, sexp_src source, int source_index, char *ms
23062305
char *object_name(int obj)
23072306
{
23082307
static char text[80];
2309-
waypoint_list *wp_list;
2310-
int waypoint_num;
23112308

23122309
if (!query_valid_object(obj))
23132310
return "*none*";
@@ -2318,9 +2315,7 @@ char *object_name(int obj)
23182315
return Ships[Objects[obj].instance].ship_name;
23192316

23202317
case OBJ_WAYPOINT:
2321-
wp_list = find_waypoint_list_with_instance(Objects[obj].instance, &waypoint_num);
2322-
Assert(wp_list != NULL);
2323-
sprintf(text, "%s:%d", wp_list->get_name(), waypoint_num + 1);
2318+
waypoint_stuff_name(text, Objects[obj].instance);
23242319
return text;
23252320

23262321
case OBJ_POINT:

fred2/orienteditor.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,7 @@ BOOL orient_editor::OnInitDialog()
129129
index[total++] = objnum;
130130

131131
} else if (ptr->type == OBJ_WAYPOINT) {
132-
int waypoint_num;
133-
waypoint_list *wp_list = find_waypoint_list_with_instance(ptr->instance, &waypoint_num);
134-
Assert(wp_list != NULL);
135-
sprintf(text, "%s:%d", wp_list->get_name(), waypoint_num + 1);
136-
132+
waypoint_stuff_name(text, ptr->instance);
137133
box->AddString(text);
138134
index[total++] = objnum;
139135

fred2/sexp_tree.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6522,14 +6522,14 @@ sexp_list_item *sexp_tree::get_listing_opf_subsystem_type(int parent_node)
65226522

65236523
sexp_list_item *sexp_tree::get_listing_opf_point()
65246524
{
6525-
char buf[NAME_LENGTH+8];
6525+
char buf[NAME_LENGTH];
65266526
sexp_list_item head;
65276527

65286528
for (const auto &ii: Waypoint_lists)
65296529
{
6530-
for (int j = 0; (uint) j < ii.get_waypoints().size(); ++j)
6530+
for (const auto &jj: ii.get_waypoints())
65316531
{
6532-
sprintf(buf, "%s:%d", ii.get_name(), j + 1);
6532+
waypoint_stuff_name(buf, jj);
65336533
head.add_data(buf);
65346534
}
65356535
}

fred2/ship_select.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ BOOL ship_select::OnInitDialog()
162162

163163
void ship_select::create_list()
164164
{
165-
SCP_string text;
166165
object *ptr;
167166

168167
update_status(true);
@@ -213,11 +212,9 @@ void ship_select::create_list()
213212
{
214213
if (ptr->type == OBJ_WAYPOINT)
215214
{
216-
int waypoint_num;
217-
waypoint_list *wp_list = find_waypoint_list_with_instance(ptr->instance, &waypoint_num);
218-
Assert(wp_list != NULL);
219-
sprintf(text, "%s:%d", wp_list->get_name(), waypoint_num + 1);
220-
m_ship_list.AddString(text.c_str());
215+
char text[NAME_LENGTH];
216+
waypoint_stuff_name(text, ptr->instance);
217+
m_ship_list.AddString(text);
221218
obj_index.push_back(OBJ_INDEX(ptr));
222219
if (ptr->flags[Object::Object_Flags::Temp_marked])
223220
m_ship_list.SetSel((int)obj_index.size());

fred2/waypointpathdlg.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,10 @@ int waypoint_path_dlg::update_data(int redraw)
272272
ai_update_goal_references(sexp_ref_type::WAYPOINT_PATH, old_name, str);
273273

274274
for (auto &wpt: cur_waypoint_list->get_waypoints()) {
275-
char old_buf[NAME_LENGTH + 8];
276-
char new_buf[NAME_LENGTH + 8];
277-
sprintf(old_buf, "%s:%d", old_name, wpt.get_index() + 1);
278-
sprintf(new_buf, "%s:%d", str, wpt.get_index() + 1);
275+
char old_buf[NAME_LENGTH];
276+
char new_buf[NAME_LENGTH];
277+
waypoint_stuff_name(old_buf, old_name, wpt.get_index() + 1);
278+
waypoint_stuff_name(new_buf, str, wpt.get_index() + 1);
279279
update_sexp_references(old_buf, new_buf);
280280
ai_update_goal_references(sexp_ref_type::WAYPOINT, old_buf, new_buf);
281281
}

qtfred/src/mission/Editor.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,7 @@ int Editor::common_object_delete(int obj) {
10101010
waypoint* wpt = find_waypoint_with_instance(Objects[obj].instance);
10111011
Assert(wpt != NULL);
10121012
waypoint_list* wp_list = wpt->get_parent_list();
1013-
int index = calc_waypoint_list_index(Objects[obj].instance);
1014-
int count = (int) wp_list->get_waypoints().size();
1013+
auto count = wp_list->get_waypoints().size();
10151014

10161015
// we'll end up deleting the path, so check for path references
10171016
if (count == 1) {
@@ -1023,7 +1022,7 @@ int Editor::common_object_delete(int obj) {
10231022
}
10241023

10251024
// check for waypoint references
1026-
sprintf(msg, "%s:%d", wp_list->get_name(), index + 1);
1025+
waypoint_stuff_name(msg, *wpt);
10271026
name = msg;
10281027
r = reference_handler(name, sexp_ref_type::WAYPOINT, obj);
10291028
if (r) {
@@ -2098,7 +2097,7 @@ int Editor::global_error_check_impl() {
20982097
return internal_error("Object references an illegal waypoint number in path");
20992098
}
21002099

2101-
sprintf(buf, "%s:%d", wp_list->get_name(), waypoint_num + 1);
2100+
waypoint_stuff_name(buf, i);
21022101
names[obj_count] = new char[strlen(buf) + 1];
21032102
strcpy(names[obj_count], buf);
21042103
err_flags[obj_count] = 1;
@@ -2440,8 +2439,8 @@ int Editor::global_error_check_impl() {
24402439
}
24412440
}
24422441

2443-
for (j = 0; (uint) j < ii.get_waypoints().size(); j++) {
2444-
sprintf(buf, "%s:%d", ii.get_name(), j + 1);
2442+
for (const auto &jj: ii.get_waypoints()) {
2443+
waypoint_stuff_name(buf, jj);
24452444
for (z = 0; z < obj_count; z++) {
24462445
if (names[z]) {
24472446
if (!stricmp(names[z], buf)) {

0 commit comments

Comments
 (0)