3333#include " starfield/starfield.h" // stars_init, stars_pre_level_init, stars_post_level_init
3434#include " hud/hudsquadmsg.h"
3535#include " globalincs/linklist.h"
36+ #include " globalincs/utility.h"
3637
3738#include " ui/QtGraphicsOperations.h"
3839
@@ -251,7 +252,7 @@ void Editor::maybeUseAutosave(std::string& filepath)
251252
252253bool Editor::loadMission (const std::string& mission_name, int flags) {
253254 char name[512 ], * old_name;
254- int i, j, k, ob;
255+ int i, j, ob;
255256 object* objp;
256257
257258 // activate the localizer hash table
@@ -387,11 +388,10 @@ bool Editor::loadMission(const std::string& mission_name, int flags) {
387388 update_sexp_references (old_name, name);
388389 ai_update_goal_references (sexp_ref_type::SHIP , old_name, name);
389390 update_texture_replacements (old_name, name);
390- for (k = 0 ; k < Num_reinforcements; k++) {
391- if (!strcmp (old_name, Reinforcements[k].name )) {
392- Assert (strlen (name) < NAME_LENGTH );
393- strcpy_s (Reinforcements[k].name , name);
394- }
391+ int k = find_item_with_string (Reinforcements, &reinforcements::name, old_name);
392+ if (k >= 0 ) {
393+ Assert (strlen (name) < NAME_LENGTH );
394+ strcpy_s (Reinforcements[k].name , name);
395395 }
396396
397397 strcpy_s (Ships[Wings[i].ship_index [j]].ship_name , name);
@@ -956,7 +956,7 @@ int Editor::getNumMarked() {
956956}
957957int Editor::dup_object (object* objp) {
958958
959- int i, j, n, inst, obj = -1 ;
959+ int i, n, inst, obj = -1 ;
960960 ai_info* aip1, * aip2;
961961 object* objp1, * objp2;
962962 ship_subsys* subp1, * subp2;
@@ -1018,17 +1018,10 @@ int Editor::dup_object(object* objp) {
10181018 subp2 = GET_NEXT (subp2);
10191019 }
10201020
1021- for (i = 0 ; i < Num_reinforcements; i++) {
1022- if (!stricmp (Reinforcements[i].name , Ships[inst].ship_name )) {
1023- if (Num_reinforcements < MAX_REINFORCEMENTS ) {
1024- j = Num_reinforcements++;
1025- strcpy_s (Reinforcements[j].name , Ships[n].ship_name );
1026- Reinforcements[j].type = Reinforcements[i].type ;
1027- Reinforcements[j].uses = Reinforcements[i].uses ;
1028- }
1029-
1030- break ;
1031- }
1021+ i = find_item_with_string (Reinforcements, &reinforcements::name, Ships[inst].ship_name );
1022+ if (i >= 0 ) {
1023+ Reinforcements.push_back (Reinforcements[i]);
1024+ strcpy_s (Reinforcements.back ().name , Ships[n].ship_name );
10321025 }
10331026
10341027 } else if (objp->type == OBJ_WAYPOINT ) {
@@ -1184,12 +1177,7 @@ int Editor::common_object_delete(int obj) {
11841177 invalidate_references (name, sexp_ref_type::SHIP );
11851178 }
11861179
1187- for (i = 0 ; i < Num_reinforcements; i++) {
1188- if (!stricmp (name, Reinforcements[i].name )) {
1189- delete_reinforcement (i);
1190- break ;
1191- }
1192- }
1180+ delete_reinforcement (name);
11931181
11941182 // check if any ship is docked with this ship and break dock if so
11951183 while (object_is_docked (&Objects[obj])) {
@@ -1396,13 +1384,8 @@ int Editor::reference_handler(const char* name, sexp_ref_type type, int obj) {
13961384 return 0 ;
13971385 }
13981386
1399- for (n = 0 ; n < Num_reinforcements; n++) {
1400- if (!stricmp (name, Reinforcements[n].name )) {
1401- break ;
1402- }
1403- }
1404-
1405- if (n < Num_reinforcements) {
1387+ n = find_item_with_string (Reinforcements, &reinforcements::name, name);
1388+ if (n >= 0 ) {
14061389 sprintf (msg, " Ship \" %s\" is a reinforcement unit.\n "
14071390 " Do you want to delete it anyway?" , name);
14081391
@@ -1639,11 +1622,9 @@ int Editor::invalidate_references(const char* name, sexp_ref_type type) {
16391622 update_sexp_references (name, new_name);
16401623 ai_update_goal_references (type, name, new_name);
16411624 update_texture_replacements (name, new_name);
1642- for (i = 0 ; i < Num_reinforcements; i++) {
1643- if (!stricmp (name, Reinforcements[i].name )) {
1644- strcpy_s (Reinforcements[i].name , new_name);
1645- }
1646- }
1625+ i = find_item_with_string (Reinforcements, &reinforcements::name, name);
1626+ if (i >= 0 )
1627+ strcpy_s (Reinforcements[i].name , new_name);
16471628
16481629 return 0 ;
16491630}
@@ -1670,8 +1651,6 @@ void Editor::update_texture_replacements(const char* old_name, const char* new_n
16701651 }
16711652}
16721653int Editor::rename_ship (int ship, const char * name) {
1673- int i;
1674-
16751654 Assert (ship >= 0 );
16761655 Assert (strlen (name) < NAME_LENGTH );
16771656
@@ -1682,44 +1661,34 @@ int Editor::rename_ship(int ship, const char* name) {
16821661 update_sexp_references (Ships[ship].ship_name , name);
16831662 ai_update_goal_references (sexp_ref_type::SHIP , Ships[ship].ship_name , name);
16841663 update_texture_replacements (Ships[ship].ship_name , name);
1685- for (i = 0 ; i < Num_reinforcements; i++) {
1686- if (!stricmp (Ships[ship].ship_name , Reinforcements[i].name )) {
1687- strcpy_s (Reinforcements[i].name , name);
1688- }
1689- }
1664+ int i = find_item_with_string (Reinforcements, &reinforcements::name, Ships[ship].ship_name );
1665+ if (i >= 0 )
1666+ strcpy_s (Reinforcements[i].name , name);
16901667
16911668 strcpy_s (Ships[ship].ship_name , name);
16921669
16931670 missionChanged ();
16941671
16951672 return 0 ;
16961673}
1697- void Editor::delete_reinforcement (int num) {
1698- int i;
1699-
1700- for (i = num; i < Num_reinforcements - 1 ; i++) {
1701- Reinforcements[i] = Reinforcements[i + 1 ];
1702- }
1674+ void Editor::delete_reinforcement (const char * name) {
1675+ int i = find_item_with_string (Reinforcements, &reinforcements::name, name);
1676+ if (i < 0 )
1677+ return ;
17031678
1704- Num_reinforcements-- ;
1679+ Reinforcements. erase (Reinforcements. begin () + i) ;
17051680 missionChanged ();
17061681}
17071682int Editor::check_wing_dependencies (int wing_num) {
17081683 const char * name = Wings[wing_num].name ;
17091684 return reference_handler (name, sexp_ref_type::WING , -1 );
17101685}
17111686int Editor::set_reinforcement (const char * name, int state) {
1712- int i, index, cur = -1 ;
1713-
1714- for (i = 0 ; i < Num_reinforcements; i++) {
1715- if (!stricmp (Reinforcements[i].name , name)) {
1716- cur = i;
1717- }
1718- }
1687+ int index;
1688+ int cur = find_item_with_string (Reinforcements, &reinforcements::name, name);
17191689
17201690 if (!state && (cur != -1 )) {
1721- Num_reinforcements--;
1722- Reinforcements[cur] = Reinforcements[Num_reinforcements];
1691+ Reinforcements.erase (Reinforcements.begin () + cur);
17231692
17241693 // clear the ship/wing flag for this reinforcement
17251694 index = ship_name_lookup (name);
@@ -1739,14 +1708,15 @@ int Editor::set_reinforcement(const char* name, int state) {
17391708 return -1 ;
17401709 }
17411710
1742- if (state && (cur == -1 ) && (Num_reinforcements < MAX_REINFORCEMENTS ) ) {
1711+ if (state && (cur == -1 )) {
17431712 Assert (strlen (name) < NAME_LENGTH );
1744- strcpy_s (Reinforcements[Num_reinforcements].name , name);
1745- Reinforcements[Num_reinforcements].uses = 1 ;
1746- Reinforcements[Num_reinforcements].arrival_delay = 0 ;
1747- memset (Reinforcements[Num_reinforcements].no_messages , 0 , MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
1748- memset (Reinforcements[Num_reinforcements].yes_messages , 0 , MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
1749- Num_reinforcements++;
1713+ reinforcements reinforcement;
1714+ strcpy_s (reinforcement.name , name);
1715+ reinforcement.uses = 1 ;
1716+ reinforcement.arrival_delay = 0 ;
1717+ memset (reinforcement.no_messages , 0 , MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
1718+ memset (reinforcement.yes_messages , 0 , MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
1719+ Reinforcements.push_back (std::move (reinforcement));
17501720
17511721 // set the reinforcement flag on the ship or wing
17521722 index = ship_name_lookup (name);
@@ -2652,21 +2622,17 @@ int Editor::global_error_check_impl() {
26522622 }
26532623 }
26542624
2655- if (Num_reinforcements > MAX_REINFORCEMENTS ) {
2656- return internal_error (" Number of reinforcements exceeds max limit" );
2657- }
2658-
2659- for (i = 0 ; i < Num_reinforcements; i++) {
2625+ for (const auto &reinforcement : Reinforcements) {
26602626 z = 0 ;
26612627 for (ship = 0 ; ship < MAX_SHIPS ; ship++) {
2662- if ((Ships[ship].objnum >= 0 ) && !stricmp (Ships[ship].ship_name , Reinforcements[i] .name )) {
2628+ if ((Ships[ship].objnum >= 0 ) && !stricmp (Ships[ship].ship_name , reinforcement .name )) {
26632629 z = 1 ;
26642630 break ;
26652631 }
26662632 }
26672633
26682634 for (wing = 0 ; wing < MAX_WINGS ; wing++) {
2669- if (Wings[wing].wave_count && !stricmp (Wings[wing].name , Reinforcements[i] .name )) {
2635+ if (Wings[wing].wave_count && !stricmp (Wings[wing].name , reinforcement .name )) {
26702636 z = 1 ;
26712637 break ;
26722638 }
0 commit comments