3131#include " starfield/starfield.h" // stars_init, stars_pre_level_init, stars_post_level_init
3232#include " hud/hudsquadmsg.h"
3333#include " globalincs/linklist.h"
34+ #include " globalincs/utility.h"
3435
3536#include " ui/QtGraphicsOperations.h"
3637
@@ -249,7 +250,7 @@ void Editor::maybeUseAutosave(std::string& filepath)
249250
250251bool Editor::loadMission (const std::string& mission_name, int flags) {
251252 char name[512 ], * old_name;
252- int i, j, k, ob;
253+ int i, j, ob;
253254 object* objp;
254255
255256 // 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 // bash it again so that we handle display names if needed
@@ -917,7 +917,7 @@ int Editor::getNumMarked() {
917917}
918918int Editor::dup_object (object* objp) {
919919
920- int i, j, n, inst, obj = -1 ;
920+ int i, n, inst, obj = -1 ;
921921 ai_info* aip1, * aip2;
922922 object* objp1, * objp2;
923923 ship_subsys* subp1, * subp2;
@@ -979,17 +979,10 @@ int Editor::dup_object(object* objp) {
979979 subp2 = GET_NEXT (subp2);
980980 }
981981
982- for (i = 0 ; i < Num_reinforcements; i++) {
983- if (!stricmp (Reinforcements[i].name , Ships[inst].ship_name )) {
984- if (Num_reinforcements < MAX_REINFORCEMENTS ) {
985- j = Num_reinforcements++;
986- strcpy_s (Reinforcements[j].name , Ships[n].ship_name );
987- Reinforcements[j].type = Reinforcements[i].type ;
988- Reinforcements[j].uses = Reinforcements[i].uses ;
989- }
990-
991- break ;
992- }
982+ i = find_item_with_string (Reinforcements, &reinforcements::name, Ships[inst].ship_name );
983+ if (i >= 0 ) {
984+ Reinforcements.push_back (Reinforcements[i]);
985+ strcpy_s (Reinforcements.back ().name , Ships[n].ship_name );
993986 }
994987
995988 } else if (objp->type == OBJ_WAYPOINT ) {
@@ -1145,12 +1138,7 @@ int Editor::common_object_delete(int obj) {
11451138 invalidate_references (name, sexp_ref_type::SHIP );
11461139 }
11471140
1148- for (i = 0 ; i < Num_reinforcements; i++) {
1149- if (!stricmp (name, Reinforcements[i].name )) {
1150- delete_reinforcement (i);
1151- break ;
1152- }
1153- }
1141+ delete_reinforcement (name);
11541142
11551143 // check if any ship is docked with this ship and break dock if so
11561144 while (object_is_docked (&Objects[obj])) {
@@ -1344,13 +1332,8 @@ int Editor::reference_handler(const char* name, sexp_ref_type type, int obj) {
13441332 return 0 ;
13451333 }
13461334
1347- for (n = 0 ; n < Num_reinforcements; n++) {
1348- if (!stricmp (name, Reinforcements[n].name )) {
1349- break ;
1350- }
1351- }
1352-
1353- if (n < Num_reinforcements) {
1335+ n = find_item_with_string (Reinforcements, &reinforcements::name, name);
1336+ if (n >= 0 ) {
13541337 sprintf (msg, " Ship \" %s\" is a reinforcement unit.\n "
13551338 " Do you want to delete it anyway?" , name);
13561339
@@ -1468,11 +1451,9 @@ int Editor::invalidate_references(const char* name, sexp_ref_type type) {
14681451 update_sexp_references (name, new_name);
14691452 ai_update_goal_references (type, name, new_name);
14701453 update_texture_replacements (name, new_name);
1471- for (i = 0 ; i < Num_reinforcements; i++) {
1472- if (!stricmp (name, Reinforcements[i].name )) {
1473- strcpy_s (Reinforcements[i].name , new_name);
1474- }
1475- }
1454+ i = find_item_with_string (Reinforcements, &reinforcements::name, name);
1455+ if (i >= 0 )
1456+ strcpy_s (Reinforcements[i].name , new_name);
14761457
14771458 return 0 ;
14781459}
@@ -1499,8 +1480,6 @@ void Editor::update_texture_replacements(const char* old_name, const char* new_n
14991480 }
15001481}
15011482int Editor::rename_ship (int ship, const char * name) {
1502- int i;
1503-
15041483 Assert (ship >= 0 );
15051484 Assert (strlen (name) < NAME_LENGTH );
15061485
@@ -1511,11 +1490,9 @@ int Editor::rename_ship(int ship, const char* name) {
15111490 update_sexp_references (Ships[ship].ship_name , name);
15121491 ai_update_goal_references (sexp_ref_type::SHIP , Ships[ship].ship_name , name);
15131492 update_texture_replacements (Ships[ship].ship_name , name);
1514- for (i = 0 ; i < Num_reinforcements; i++) {
1515- if (!stricmp (Ships[ship].ship_name , Reinforcements[i].name )) {
1516- strcpy_s (Reinforcements[i].name , name);
1517- }
1518- }
1493+ int i = find_item_with_string (Reinforcements, &reinforcements::name, Ships[ship].ship_name );
1494+ if (i >= 0 )
1495+ strcpy_s (Reinforcements[i].name , name);
15191496
15201497 strcpy_s (Ships[ship].ship_name , name);
15211498
@@ -1537,32 +1514,24 @@ int Editor::rename_ship(int ship, const char* name) {
15371514
15381515 return 0 ;
15391516}
1540- void Editor::delete_reinforcement (int num) {
1541- int i;
1542-
1543- for (i = num; i < Num_reinforcements - 1 ; i++) {
1544- Reinforcements[i] = Reinforcements[i + 1 ];
1545- }
1517+ void Editor::delete_reinforcement (const char * name) {
1518+ int i = find_item_with_string (Reinforcements, &reinforcements::name, name);
1519+ if (i < 0 )
1520+ return ;
15461521
1547- Num_reinforcements-- ;
1522+ Reinforcements. erase (Reinforcements. begin () + i) ;
15481523 missionChanged ();
15491524}
15501525int Editor::check_wing_dependencies (int wing_num) {
15511526 const char * name = Wings[wing_num].name ;
15521527 return reference_handler (name, sexp_ref_type::WING , -1 );
15531528}
15541529int Editor::set_reinforcement (const char * name, int state) {
1555- int i, index, cur = -1 ;
1556-
1557- for (i = 0 ; i < Num_reinforcements; i++) {
1558- if (!stricmp (Reinforcements[i].name , name)) {
1559- cur = i;
1560- }
1561- }
1530+ int index;
1531+ int cur = find_item_with_string (Reinforcements, &reinforcements::name, name);
15621532
15631533 if (!state && (cur != -1 )) {
1564- Num_reinforcements--;
1565- Reinforcements[cur] = Reinforcements[Num_reinforcements];
1534+ Reinforcements.erase (Reinforcements.begin () + cur);
15661535
15671536 // clear the ship/wing flag for this reinforcement
15681537 index = ship_name_lookup (name);
@@ -1582,14 +1551,15 @@ int Editor::set_reinforcement(const char* name, int state) {
15821551 return -1 ;
15831552 }
15841553
1585- if (state && (cur == -1 ) && (Num_reinforcements < MAX_REINFORCEMENTS ) ) {
1554+ if (state && (cur == -1 )) {
15861555 Assert (strlen (name) < NAME_LENGTH );
1587- strcpy_s (Reinforcements[Num_reinforcements].name , name);
1588- Reinforcements[Num_reinforcements].uses = 1 ;
1589- Reinforcements[Num_reinforcements].arrival_delay = 0 ;
1590- memset (Reinforcements[Num_reinforcements].no_messages , 0 , MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
1591- memset (Reinforcements[Num_reinforcements].yes_messages , 0 , MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
1592- Num_reinforcements++;
1556+ reinforcements reinforcement;
1557+ strcpy_s (reinforcement.name , name);
1558+ reinforcement.uses = 1 ;
1559+ reinforcement.arrival_delay = 0 ;
1560+ memset (reinforcement.no_messages , 0 , MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
1561+ memset (reinforcement.yes_messages , 0 , MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
1562+ Reinforcements.push_back (std::move (reinforcement));
15931563
15941564 // set the reinforcement flag on the ship or wing
15951565 index = ship_name_lookup (name);
0 commit comments