diff --git a/code/hud/hudsquadmsg.cpp b/code/hud/hudsquadmsg.cpp index 71369307009..32af4e8751a 100644 --- a/code/hud/hudsquadmsg.cpp +++ b/code/hud/hudsquadmsg.cpp @@ -1723,18 +1723,18 @@ int hud_squadmsg_send_wing_command( int wingnum, int command, int send_message, // return number of available reinforcements, 0 if none available int hud_squadmsg_reinforcements_available(int team) { - int i, count = 0; + int count = 0; - for (i = 0; i < Num_reinforcements; i++) { + for (const auto &reinforcement: Reinforcements) { int wingnum; // no more left - if ( Reinforcements[i].num_uses >= Reinforcements[i].uses ){ + if (reinforcement.num_uses >= reinforcement.uses ){ continue; } // incorrect team - if ( team != ship_get_reinforcement_team(i) ){ + if ( team != ship_get_reinforcement_team(reinforcement) ){ continue; } @@ -1742,7 +1742,7 @@ int hud_squadmsg_reinforcements_available(int team) // Goober5000 - if it can't arrive, it doesn't count. This should check // for SEXP_FALSE as well as SEXP_KNOWN_FALSE, otherwise you end up with // a reinforcement menu containing no valid selections. - if ( (wingnum = wing_name_lookup(Reinforcements[i].name, 1)) != -1 ) { + if ( (wingnum = wing_name_lookup(reinforcement.name, 1)) != -1 ) { Assert ( Wings[wingnum].arrival_cue >= 0 ); if ( Sexp_nodes[Wings[wingnum].arrival_cue].value == SEXP_FALSE || Sexp_nodes[Wings[wingnum].arrival_cue].value == SEXP_KNOWN_FALSE ){ @@ -1751,7 +1751,7 @@ int hud_squadmsg_reinforcements_available(int team) } else { p_object *p_objp; - p_objp = mission_parse_get_arrival_ship( Reinforcements[i].name ); + p_objp = mission_parse_get_arrival_ship( reinforcement.name ); if ( p_objp != NULL ) { if ( Sexp_nodes[p_objp->arrival_cue].value == SEXP_FALSE || Sexp_nodes[p_objp->arrival_cue].value == SEXP_KNOWN_FALSE ){ @@ -1973,23 +1973,20 @@ void hud_squadmsg_msg_all_fighters() // called to actually bring in a reinforcement. For single player games, always gets called. // for multiplayer games, always called on the server side. Clients should never get here -void hud_squadmsg_call_reinforcement(int reinforcement_num, int /*player_num*/) +void hud_squadmsg_call_reinforcement(reinforcements &reinforcement, int /*player_num*/) { int i, delay; - reinforcements *rp; p_object *p_objp; - rp = &Reinforcements[reinforcement_num]; - // safety net mainly for multiplayer servers in case some odd data desync occurs between // server and clients - if ( MULTIPLAYER_MASTER && (rp->num_uses >= rp->uses) ) { + if ( MULTIPLAYER_MASTER && (reinforcement.num_uses >= reinforcement.uses) ) { return; } // check to see if the reinforcement called was a wing. for (i = 0; i < Num_wings; i++ ) { - if ( !stricmp(rp->name, Wings[i].name) ) { + if ( !stricmp(reinforcement.name, Wings[i].name) ) { // if the wing is currently present, skip this request so we don't waste a "use" if (Wings[i].current_count > 0) { return; @@ -2001,7 +1998,7 @@ void hud_squadmsg_call_reinforcement(int reinforcement_num, int /*player_num*/) Wings[i].flags.set(Ship::Wing_Flags::Reset_reinforcement); // set up the arrival delay. If it is 0, then make is some random number of seconds - delay = rp->arrival_delay; + delay = reinforcement.arrival_delay; if ( delay == 0 ) delay = (int)(frand() * 3.0) + 3; Wings[i].arrival_delay = timestamp(delay * 1000); @@ -2012,14 +2009,14 @@ void hud_squadmsg_call_reinforcement(int reinforcement_num, int /*player_num*/) // if we found no wing name that matched the reinforcement name, then look for a ship // of the same name if ( i == Num_wings ) { - p_objp = mission_parse_get_arrival_ship( rp->name ); + p_objp = mission_parse_get_arrival_ship( reinforcement.name ); if ( p_objp ) { // by resetting the reinforcement flag, we will allow code which normally handles arrivals // to make this reinforcement arrive. Doing so keeps the data structures clean. p_objp->flags.remove(Mission::Parse_Object_Flags::SF_Reinforcement); // set up the arrival delay - delay = rp->arrival_delay; + delay = reinforcement.arrival_delay; if ( delay == 0 ) delay = (int)(frand() * 3.0) + 3; // between 3 and 6 seconds to arrive p_objp->arrival_delay = timestamp(delay * 1000); @@ -2031,7 +2028,7 @@ void hud_squadmsg_call_reinforcement(int reinforcement_num, int /*player_num*/) // increment the number of times this is used. Incremented here on single player and multiplayer // server side only. Clients keep track of own count when they actually call something in. - rp->num_uses++; + reinforcement.num_uses++; // commented out on 9/9/98 because these messages simply are not used /* @@ -2043,31 +2040,30 @@ void hud_squadmsg_call_reinforcement(int reinforcement_num, int /*player_num*/) break; //if ( i > 0 ) - // message_send_to_player( rp->yes_messages[Random::next(i)], rp->name, MESSAGE_PRIORITY_NORMAL, HUD_SOURCE_FRIENDLY ); + // message_send_to_player( rp->yes_messages[Random::next(i)], reinforcement.name, MESSAGE_PRIORITY_NORMAL, HUD_SOURCE_FRIENDLY ); */ - mission_log_add_entry(LOG_PLAYER_CALLED_FOR_REINFORCEMENT, rp->name, NULL); + mission_log_add_entry(LOG_PLAYER_CALLED_FOR_REINFORCEMENT, reinforcement.name, nullptr); } // function to display a list of reinforcements available to the player void hud_squadmsg_reinforcement_select() { - int i, k, wingnum; - reinforcements *rp; + int i = -1, wingnum; if ( Num_menu_items == -1 ) { Num_menu_items = 0; - for (i = 0; i < Num_reinforcements; i++) { - rp = &Reinforcements[i]; - SCP_string rp_name = rp->name; + for (const auto &reinforcement: Reinforcements) { + ++i; // start at 0; increment at top of loop due to continues + SCP_string r_name = reinforcement.name; // copy it // don't put reinforcements onto the list that have already been used up. - if ( rp->num_uses >= rp->uses ){ + if (reinforcement.num_uses >= reinforcement.uses){ continue; } // don't put items which are not on my team - if((Player_ship != NULL) && (ship_get_reinforcement_team(i) != Player_ship->team)){ + if((Player_ship != nullptr) && (ship_get_reinforcement_team(reinforcement) != Player_ship->team)){ continue; } @@ -2075,18 +2071,18 @@ void hud_squadmsg_reinforcement_select() // Goober5000 - if it can't arrive, it doesn't count. This should check // for SEXP_FALSE as well as SEXP_KNOWN_FALSE, otherwise you end up with // a reinforcement menu containing no valid selections. - if ( (wingnum = wing_name_lookup(rp->name, 1)) != -1 ) { + if ( (wingnum = wing_name_lookup(reinforcement.name, 1)) != -1 ) { Assert ( Wings[wingnum].arrival_cue >= 0 ); if ( Sexp_nodes[Wings[wingnum].arrival_cue].value == SEXP_FALSE || Sexp_nodes[Wings[wingnum].arrival_cue].value == SEXP_KNOWN_FALSE ){ continue; } - end_string_at_first_hash_symbol(rp_name); + end_string_at_first_hash_symbol(r_name); } else { p_object *p_objp; - p_objp = mission_parse_get_arrival_ship( rp->name ); + p_objp = mission_parse_get_arrival_ship(reinforcement.name ); if ( p_objp != NULL ) { if ( Sexp_nodes[p_objp->arrival_cue].value == SEXP_FALSE || Sexp_nodes[p_objp->arrival_cue].value == SEXP_KNOWN_FALSE ){ @@ -2097,15 +2093,15 @@ void hud_squadmsg_reinforcement_select() continue; } - rp_name = p_objp->get_display_name(); // this will handle getting rid of the hash if necessary + r_name = p_objp->get_display_name(); // this will handle getting rid of the hash if necessary } Assert ( Num_menu_items < MAX_MENU_ITEMS ); - MsgItems[Num_menu_items].text = std::move(rp_name); + MsgItems[Num_menu_items].text = std::move(r_name); MsgItems[Num_menu_items].instance = i; MsgItems[Num_menu_items].active = 0; - if ( rp->flags & RF_IS_AVAILABLE ) { + if ( reinforcement.flags & RF_IS_AVAILABLE ) { MsgItems[Num_menu_items].active = 1; } @@ -2115,7 +2111,7 @@ void hud_squadmsg_reinforcement_select() // hud_squadmsg_display_menu( "Select Reinforcement" ); strcpy_s(Squad_msg_title, XSTR( "Select Ship/Wing", 319)); // AL 11-14-97: Reinforcement didn't fit, so using this for now - k = hud_squadmsg_get_key(); + int k = hud_squadmsg_get_key(); if (k != -1) { int rnum; @@ -2135,7 +2131,7 @@ void hud_squadmsg_reinforcement_select() Reinforcements[rnum].num_uses++; // increment this variable here since clients need to maintain a valid count send_player_order_packet(SQUAD_MSG_REINFORCEMENT, rnum, 0); } else { - hud_squadmsg_call_reinforcement(rnum); + hud_squadmsg_call_reinforcement(Reinforcements[rnum]); } } } diff --git a/code/hud/hudsquadmsg.h b/code/hud/hudsquadmsg.h index efd8067e267..7cf6eb016c2 100644 --- a/code/hud/hudsquadmsg.h +++ b/code/hud/hudsquadmsg.h @@ -30,6 +30,7 @@ #define MESSAGE_ALL_FIGHTERS -999 class object; +struct reinforcements; // defines for messages that can be sent from the player. Indexes into Player_orders @@ -181,7 +182,7 @@ extern void hud_squadmsg_rearm_shortcut(); extern int hud_squadmsg_send_ship_command( int shipnum, int command, int send_message, int update_history = SQUADMSG_HISTORY_ADD_ENTRY, int player_num = -1 ); extern int hud_squadmsg_send_wing_command( int wingnum, int command, int send_message, int update_history = SQUADMSG_HISTORY_ADD_ENTRY, int player_num = -1 ); extern void hud_squadmsg_send_to_all_fighters( int command, int player_num = -1 ); -extern void hud_squadmsg_call_reinforcement(int reinforcement_num, int player_num = -1); +extern void hud_squadmsg_call_reinforcement(reinforcements &reinforcement, int player_num = -1); extern int hud_squadmsg_reinforcements_available(int team); diff --git a/code/mission/missionparse.cpp b/code/mission/missionparse.cpp index 70b4f64a465..0b13c1c9bd3 100644 --- a/code/mission/missionparse.cpp +++ b/code/mission/missionparse.cpp @@ -5995,25 +5995,23 @@ void parse_messages(mission *pm, int flags) void parse_reinforcement(mission *pm) { - reinforcements *ptr; + reinforcements reinforcement; p_object *rforce_obj = NULL; int instance = -1; - Assert(Num_reinforcements < MAX_REINFORCEMENTS); Assert(pm != NULL); - ptr = &Reinforcements[Num_reinforcements]; required_string("$Name:"); - stuff_string(ptr->name, F_NAME, NAME_LENGTH); + stuff_string(reinforcement.name, F_NAME, NAME_LENGTH); - find_and_stuff("$Type:", &ptr->type, F_NAME, Reinforcement_type_names, Num_reinforcement_type_names, "reinforcement type"); + find_and_stuff("$Type:", &reinforcement.type, F_NAME, Reinforcement_type_names, Num_reinforcement_type_names, "reinforcement type"); required_string("$Num times:"); - stuff_int(&ptr->uses); - ptr->num_uses = 0; + stuff_int(&reinforcement.uses); + reinforcement.num_uses = 0; // reset the flags to 0 - ptr->flags = 0; + reinforcement.flags = 0; if ( optional_string("+Arrival delay:") ) { @@ -6021,34 +6019,34 @@ void parse_reinforcement(mission *pm) stuff_int(&delay); if (delay < 0) { - Warning(LOCATION, "Cannot have arrival delay < 0 on reinforcement %s", ptr->name); + Warning(LOCATION, "Cannot have arrival delay < 0 on reinforcement %s", reinforcement.name); delay = 0; } - ptr->arrival_delay = delay; + reinforcement.arrival_delay = delay; } if ( optional_string("+No Messages:") ){ - stuff_string_list( ptr->no_messages, MAX_REINFORCEMENT_MESSAGES ); + stuff_string_list( reinforcement.no_messages, MAX_REINFORCEMENT_MESSAGES ); } if ( optional_string("+Yes Messages:") ){ - stuff_string_list( ptr->yes_messages, MAX_REINFORCEMENT_MESSAGES ); + stuff_string_list( reinforcement.yes_messages, MAX_REINFORCEMENT_MESSAGES ); } // sanity check on the names of reinforcements - rforce_obj = mission_parse_find_parse_object(ptr->name); + rforce_obj = mission_parse_find_parse_object(reinforcement.name); if (rforce_obj == NULL) { - if ((instance = wing_name_lookup(ptr->name, 1)) == -1) { - Warning(LOCATION, "Reinforcement %s not found as ship or wing", ptr->name); + if ((instance = wing_name_lookup(reinforcement.name, 1)) == -1) { + Warning(LOCATION, "Reinforcement %s not found as ship or wing", reinforcement.name); return; } } else { // Individual ships in wings can't be reinforcements - FUBAR if (rforce_obj->wingnum >= 0) { - Warning(LOCATION, "Reinforcement %s is part of a wing - Ignoring reinforcement declaration", ptr->name); + Warning(LOCATION, "Reinforcement %s is part of a wing - Ignoring reinforcement declaration", reinforcement.name); return; } else @@ -6060,10 +6058,10 @@ void parse_reinforcement(mission *pm) // now, if the reinforcement is a wing, then set the number of waves of the wing == number of // uses of the reinforcement if (instance >= 0) { - Wings[instance].num_waves = ptr->uses; + Wings[instance].num_waves = reinforcement.uses; } - Num_reinforcements++; + Reinforcements.push_back(std::move(reinforcement)); } void parse_reinforcements(mission *pm) @@ -7305,7 +7303,7 @@ void mission_init(mission *pm, bool quick_init) for (int i = 0; i < MAX_WINGS; i++) Wings[i].clear(); - Num_reinforcements = 0; + Reinforcements.clear(); Parse_props.clear(); @@ -8055,27 +8053,24 @@ int mission_set_arrival_location(anchor_t anchor, ArrivalLocation location, int /** * Mark a reinforcement as available */ -void mission_parse_mark_reinforcement_available(char *name) +void mission_parse_mark_reinforcement_available(const char *name) { - int i; - reinforcements *rp; - - for (i = 0; i < Num_reinforcements; i++) { - rp = &Reinforcements[i]; - if ( !stricmp(rp->name, name) ) { - if ( !(rp->flags & RF_IS_AVAILABLE) ) { - rp->flags |= RF_IS_AVAILABLE; + int i = find_item_with_string(Reinforcements, &reinforcements::name, name); + if (i >= 0) + { + auto &r = Reinforcements[i]; + if (!(r.flags & RF_IS_AVAILABLE)) + { + r.flags |= RF_IS_AVAILABLE; - // tell all of the clients. - if ( MULTIPLAYER_MASTER ) { - send_reinforcement_avail( i ); - } - } - return; + // tell all of the clients. + if (MULTIPLAYER_MASTER) + send_reinforcement_avail(i); } + return; } - Assert ( i < Num_reinforcements ); + Assertion(false, "Reinforcement '%s' not found!", name); } /** @@ -8101,12 +8096,9 @@ int mission_did_ship_arrive(p_object *objp, bool force_arrival) // if we're forcing the arrival, then "use" the reinforcement; otherwise don't process anything else if (force_arrival) { - for (int i = 0; i < Num_reinforcements; i++) { - auto rp = &Reinforcements[i]; - if (!stricmp(rp->name, objp->name)) { - rp->num_uses++; - break; - } + int i = find_item_with_string(Reinforcements, &reinforcements::name, objp->name); + if (i >= 0) { + Reinforcements[i].num_uses++; } } else { return -1; @@ -8358,12 +8350,9 @@ bool mission_maybe_make_wing_arrive(int wingnum, bool force_arrival) // if we're forcing the arrival, then "use" the reinforcement; otherwise don't process anything else if (force_arrival && wingp->current_count == 0) { - for (int i = 0; i < Num_reinforcements; i++) { - auto rp = &Reinforcements[i]; - if (!stricmp(rp->name, wingp->name)) { - rp->num_uses++; - break; - } + int i = find_item_with_string(Reinforcements, &reinforcements::name, wingp->name); + if (i >= 0) { + Reinforcements[i].num_uses++; } } else { // reinforcement wings skip the rest of the function diff --git a/code/missioneditor/missionsave.cpp b/code/missioneditor/missionsave.cpp index 505163dee6c..13542d26df1 100644 --- a/code/missioneditor/missionsave.cpp +++ b/code/missioneditor/missionsave.cpp @@ -4502,22 +4502,24 @@ int Fred_mission_save::save_plot_info() int Fred_mission_save::save_reinforcements() { - int i, j, type; + int j, type; fred_parse_flag = 0; required_string_fred("#Reinforcements"); parse_comments(2); - fout("\t\t;! %d total\n", Num_reinforcements); + fout("\t\t;! " SIZE_T_ARG " total\n", Reinforcements.size()); - for (i = 0; i < Num_reinforcements; i++) { + bool first_r = true; + for (const auto &reinforcement: Reinforcements) { required_string_either_fred("$Name:", "#Background bitmaps"); required_string_fred("$Name:"); - parse_comments(i ? 2 : 1); - fout(" %s", Reinforcements[i].name); + parse_comments(!first_r ? 2 : 1); + first_r = false; + fout(" %s", reinforcement.name); type = TYPE_ATTACK_PROTECT; for (j = 0; j < MAX_SHIPS; j++) - if ((Ships[j].objnum != -1) && !stricmp(Ships[j].ship_name, Reinforcements[i].name)) { + if ((Ships[j].objnum != -1) && !stricmp(Ships[j].ship_name, reinforcement.name)) { if (Ship_info[Ships[j].ship_info_index].flags[Ship::Info_Flags::Support]) type = TYPE_REPAIR_REARM; break; @@ -4529,13 +4531,13 @@ int Fred_mission_save::save_reinforcements() required_string_fred("$Num times:"); parse_comments(); - fout(" %d", Reinforcements[i].uses); + fout(" %d", reinforcement.uses); if (optional_string_fred("+Arrival Delay:", "$Name:")) parse_comments(); else fout("\n+Arrival Delay:"); - fout(" %d", Reinforcements[i].arrival_delay); + fout(" %d", reinforcement.arrival_delay); if (optional_string_fred("+No Messages:", "$Name:")) parse_comments(); @@ -4543,8 +4545,8 @@ int Fred_mission_save::save_reinforcements() fout("\n+No Messages:"); fout(" ("); for (j = 0; j < MAX_REINFORCEMENT_MESSAGES; j++) { - if (strlen(Reinforcements[i].no_messages[j])) - fout(" \"%s\"", Reinforcements[i].no_messages[j]); + if (strlen(reinforcement.no_messages[j])) + fout(" \"%s\"", reinforcement.no_messages[j]); } fout(" )"); @@ -4554,8 +4556,8 @@ int Fred_mission_save::save_reinforcements() fout("\n+Yes Messages:"); fout(" ("); for (j = 0; j < MAX_REINFORCEMENT_MESSAGES; j++) { - if (strlen(Reinforcements[i].yes_messages[j])) - fout(" \"%s\"", Reinforcements[i].yes_messages[j]); + if (strlen(reinforcement.yes_messages[j])) + fout(" \"%s\"", reinforcement.yes_messages[j]); } fout(" )"); diff --git a/code/network/multimsgs.cpp b/code/network/multimsgs.cpp index 6d1ba57db17..d45709765cf 100644 --- a/code/network/multimsgs.cpp +++ b/code/network/multimsgs.cpp @@ -4506,8 +4506,8 @@ void process_player_order_packet(ubyte *data, header *hinfo) // check to see if the type of order is a reinforcement call. If so, intercept it, and // then call them in. if ( type == SQUAD_MSG_REINFORCEMENT ) { - Assert( (index >= 0) && (index < Num_reinforcements) ); - hud_squadmsg_call_reinforcement(index, player_num); + Assert(Reinforcements.in_bounds(index)); + hud_squadmsg_call_reinforcement(Reinforcements[index], player_num); return; } @@ -7705,7 +7705,7 @@ void process_reinforcement_avail( ubyte *data, header *hinfo ) PACKET_SET_SIZE(); // sanity check for a valid reinforcement number - if ( (rnum >= 0) && (rnum < Num_reinforcements) ) { + if (Reinforcements.in_bounds(rnum)) { Reinforcements[rnum].flags |= RF_IS_AVAILABLE; } } diff --git a/code/ship/ship.cpp b/code/ship/ship.cpp index 24fedc63581..a63e270e272 100644 --- a/code/ship/ship.cpp +++ b/code/ship/ship.cpp @@ -128,7 +128,6 @@ int Ship_auto_repair = 1; // flag to indicate auto-repair of subsystem should o #endif int Num_wings = 0; -int Num_reinforcements = 0; ship Ships[MAX_SHIPS]; ship *Player_ship; @@ -312,7 +311,7 @@ ship_obj Ship_objs[MAX_SHIP_OBJS]; // array used to store ship object indexes ship_obj Ship_obj_list; // head of linked list of ship_obj structs, Standalone ship cannot be in this list or it will cause bugs. SCP_vector Ship_info; -reinforcements Reinforcements[MAX_REINFORCEMENTS]; +SCP_vector Reinforcements; SCP_vector Ship_templates; SCP_vector Ship_types; @@ -19790,23 +19789,18 @@ int wing_has_conflicting_teams(int wing_index) /** * Get the team of a reinforcement item */ -int ship_get_reinforcement_team(int r_index) +int ship_get_reinforcement_team(const reinforcements &reinforcement) { int wing_index; p_object *p_objp; - // sanity checks - Assert((r_index >= 0) && (r_index < Num_reinforcements)); - if ((r_index < 0) || (r_index >= Num_reinforcements)) - return -1; - // if the reinforcement is a ship - p_objp = mission_parse_get_arrival_ship(Reinforcements[r_index].name); + p_objp = mission_parse_get_arrival_ship(reinforcement.name); if (p_objp != NULL) return p_objp->team; // if the reinforcement is a ship - wing_index = wing_lookup(Reinforcements[r_index].name); + wing_index = wing_lookup(reinforcement.name); if (wing_index >= 0) { // go through the ship arrival list and find any ship in this wing diff --git a/code/ship/ship.h b/code/ship/ship.h index d656d936084..0a27710ca37 100644 --- a/code/ship/ship.h +++ b/code/ship/ship.h @@ -57,7 +57,6 @@ extern vec3d Original_vec_to_deader; #define MAX_SHIP_SPARKS 8 // maximum number of spark emitters on a ship #define MAX_SHIP_DETAIL_LEVELS 5 // maximum detail levels that a ship can render at -#define MAX_REINFORCEMENTS 32 // defines for 'direction' parameter of ship_select_next_primary() @@ -80,7 +79,7 @@ enum class CycleDirection { NEXT, PREV }; #define RF_IS_AVAILABLE (1<<0) // reinforcement is now available -typedef struct { +struct reinforcements { char name[NAME_LENGTH]; // ship or wing name (ship and wing names don't collide) int type; // what operations this reinforcement unit can perform int uses; // number of times reinforcemnt unit can be used @@ -89,7 +88,7 @@ typedef struct { int flags; char no_messages[MAX_REINFORCEMENT_MESSAGES][NAME_LENGTH]; // list of messages to possibly send when calling for reinforcement not available char yes_messages[MAX_REINFORCEMENT_MESSAGES][NAME_LENGTH]; // list of messages to acknowledge reinforcement on the way -} reinforcements; +}; class ship_weapon { public: @@ -1647,9 +1646,8 @@ extern char TVT_wing_names[MAX_TVT_WINGS][NAME_LENGTH]; extern int ai_paused; -extern int Num_reinforcements; extern SCP_vector Ship_info; -extern reinforcements Reinforcements[MAX_REINFORCEMENTS]; +extern SCP_vector Reinforcements; // structure definition for ship type counts. Used to give a count of the number of ships // of a particular type, and the number of times that a ship of that particular type has been @@ -1975,7 +1973,7 @@ int ship_get_turret_type(ship_subsys *subsys); int ship_get_by_signature(int sig); // get the team of a reinforcement item -int ship_get_reinforcement_team(int r_index); +int ship_get_reinforcement_team(const reinforcements &reinforcement); // page in bitmaps for all ships on a given level void ship_page_in(); diff --git a/fred2/freddoc.cpp b/fred2/freddoc.cpp index 8c1deb346bf..d788da0a48b 100644 --- a/fred2/freddoc.cpp +++ b/fred2/freddoc.cpp @@ -226,7 +226,7 @@ bool CFREDDoc::load_mission(const char *pathname, int flags) { chdir(Fred_base_dir); char name[512], *old_name; - int i, j, k, ob; + int i, j, ob; int used_pool[MAX_WEAPON_TYPES]; object *objp; @@ -321,11 +321,11 @@ bool CFREDDoc::load_mission(const char *pathname, int flags) { update_sexp_references(old_name, name); ai_update_goal_references(sexp_ref_type::SHIP, old_name, name); update_texture_replacements(old_name, name); - for (k = 0; k < Num_reinforcements; k++) - if (!strcmp(old_name, Reinforcements[k].name)) { - Assert(strlen(name) < NAME_LENGTH); - strcpy_s(Reinforcements[k].name, name); - } + int k = find_item_with_string(Reinforcements, &reinforcements::name, old_name); + if (k >= 0) { + Assert(strlen(name) < NAME_LENGTH); + strcpy_s(Reinforcements[k].name, name); + } strcpy_s(Ships[Wings[i].ship_index[j]].ship_name, name); } diff --git a/fred2/fredview.cpp b/fred2/fredview.cpp index 2281c2b290c..26b9cb40405 100644 --- a/fred2/fredview.cpp +++ b/fred2/fredview.cpp @@ -2954,21 +2954,17 @@ int CFREDView::global_error_check() } } - if (Num_reinforcements > MAX_REINFORCEMENTS){ - return internal_error("Number of reinforcements exceeds max limit"); - } - - for (i=0; i= 0) && !stricmp(Ships[ship].ship_name, Reinforcements[i].name)) { + if ((Ships[ship].objnum >= 0) && !stricmp(Ships[ship].ship_name, reinforcement.name)) { z = 1; break; } } for (wing=0; wing= 0) { + Reinforcements.push_back(Reinforcements[i]); + strcpy_s(Reinforcements.back().name, Ships[n].ship_name); + } } else if (objp->type == OBJ_WAYPOINT) { obj = create_waypoint(&objp->pos, waypoint_instance); @@ -1345,11 +1339,7 @@ int common_object_delete(int obj) invalidate_references(name, sexp_ref_type::SHIP); } - for (i = 0; i < Num_reinforcements; i++) - if (!stricmp(name, Reinforcements[i].name)) { - delete_reinforcement(i); - break; - } + delete_reinforcement(name); // check if any ship is docked with this ship and break dock if so while (object_is_docked(&Objects[obj])) { @@ -1416,14 +1406,13 @@ void delete_marked() Update_window = 1; } -void delete_reinforcement(int num) +void delete_reinforcement(const char *name) { - int i; - - for (i=num; i= 0); Assert(strlen(name) < NAME_LENGTH); @@ -1838,10 +1820,9 @@ int rename_ship(int ship, const char *name) update_sexp_references(Ships[ship].ship_name, name); ai_update_goal_references(sexp_ref_type::SHIP, Ships[ship].ship_name, name); update_texture_replacements(Ships[ship].ship_name, name); - for (i=0; i= 0) + strcpy_s(Reinforcements[i].name, name); strcpy_s(Ships[ship].ship_name, name); if (ship == cur_ship) @@ -1853,16 +1834,14 @@ int rename_ship(int ship, const char *name) int invalidate_references(const char *name, sexp_ref_type type) { char new_name[512]; - int i; sprintf(new_name, "<%s>", name); update_sexp_references(name, new_name); ai_update_goal_references(type, name, new_name); update_texture_replacements(name, new_name); - for (i=0; i= 0) + strcpy_s(Reinforcements[i].name, new_name); return 0; } @@ -2165,11 +2144,8 @@ int reference_handler(const char *name, sexp_ref_type type, int obj) if ((type != sexp_ref_type::SHIP) && (type != sexp_ref_type::WING)) return 0; - for (n=0; n= 0) { sprintf(msg, "Ship \"%s\" is a reinforcement unit.\n" "Do you want to delete it anyway?", name); diff --git a/fred2/management.h b/fred2/management.h index 70c7f7b6cc4..7d76ff585ee 100644 --- a/fred2/management.h +++ b/fred2/management.h @@ -79,7 +79,7 @@ int delete_object(int obj); int delete_object(object* ptr); int delete_ship(int ship); void delete_marked(); -void delete_reinforcement(int num); +void delete_reinforcement(const char *name); int delete_ship_from_wing(int ship = cur_ship); int find_free_wing(); int query_object_in_wing(int obj = cur_object_index); @@ -96,7 +96,7 @@ void set_cur_wing(int wing); int gray_menu_tree(CMenu* base); int query_initial_orders_conflict(int wing); int query_initial_orders_empty(ai_goal* ai_goals); -int set_reinforcement(char* name, int state); +int set_reinforcement(const char* name, int state); int get_docking_list(int model_index); int rename_ship(int ship, const char* name); void fix_ship_name(int ship); diff --git a/fred2/reinforcementeditordlg.cpp b/fred2/reinforcementeditordlg.cpp index af06e0fa30e..e8876c895e9 100644 --- a/fred2/reinforcementeditordlg.cpp +++ b/fred2/reinforcementeditordlg.cpp @@ -64,23 +64,22 @@ END_MESSAGE_MAP() BOOL reinforcement_editor_dlg::OnInitDialog() { - int i; CListBox *box; CDialog::OnInitDialog(); theApp.init_window(&Reinforcement_wnd_data, this); box = (CListBox *) GetDlgItem(IDC_LIST); - for (i=0; iAddString(Reinforcements[i].name); + + m_reinforcements.clear(); + for (const auto &reinforcement: Reinforcements) + { + m_reinforcements.push_back(reinforcement); + box->AddString(reinforcement.name); } - m_num_reinforcements = Num_reinforcements; m_uses_spin.SetRange(1, 99); m_delay_spin.SetRange(0, 1000); update_data(); - if (Num_reinforcements == MAX_REINFORCEMENTS) - GetDlgItem(IDC_ADD) -> EnableWindow(FALSE); return TRUE; } @@ -137,7 +136,6 @@ void reinforcement_editor_dlg::save_data() UpdateData(TRUE); UpdateData(TRUE); if (cur >= 0) { - Assert(cur < m_num_reinforcements); m_reinforcements[cur].uses = m_uses; m_reinforcements[cur].arrival_delay = m_delay; @@ -153,10 +151,11 @@ int reinforcement_editor_dlg::query_modified() int i, j; save_data(); - if (Num_reinforcements != m_num_reinforcements) + if (Reinforcements.size() != m_reinforcements.size()) return 1; - for (i=0; i= 0){ @@ -332,33 +328,27 @@ void reinforcement_editor_dlg::OnAdd() } } - i = m_num_reinforcements++; - strcpy_s(m_reinforcements[i].name, dlg.name); + reinforcements reinforcement; + strcpy_s(reinforcement.name, dlg.name); ((CListBox *) GetDlgItem(IDC_LIST)) -> AddString(dlg.name); - m_reinforcements[i].type = 0; - m_reinforcements[i].uses = 1; - m_reinforcements[i].arrival_delay = 0; - memset( m_reinforcements[i].no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH ); - memset( m_reinforcements[i].yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH ); - if (m_num_reinforcements == MAX_REINFORCEMENTS){ - GetDlgItem(IDC_ADD) -> EnableWindow(FALSE); - } + reinforcement.type = 0; + reinforcement.uses = 1; + reinforcement.arrival_delay = 0; + memset( reinforcement.no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH ); + memset( reinforcement.yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH ); + m_reinforcements.push_back(std::move(reinforcement)); } } void reinforcement_editor_dlg::OnDelete() { - int i; - if (cur != -1) { ((CListBox *) GetDlgItem(IDC_LIST)) -> DeleteString(cur); - for (i=cur; i EnableWindow(FALSE); } diff --git a/fred2/reinforcementeditordlg.h b/fred2/reinforcementeditordlg.h index 9d541e623a3..28cd4f4d718 100644 --- a/fred2/reinforcementeditordlg.h +++ b/fred2/reinforcementeditordlg.h @@ -57,8 +57,7 @@ class reinforcement_editor_dlg : public CDialog //}}AFX_MSG DECLARE_MESSAGE_MAP() private: - int m_num_reinforcements; - reinforcements m_reinforcements[MAX_REINFORCEMENTS]; + SCP_vector m_reinforcements; int cur; }; diff --git a/fred2/shipeditordlg.cpp b/fred2/shipeditordlg.cpp index c2be434cb0e..5f24f387e53 100644 --- a/fred2/shipeditordlg.cpp +++ b/fred2/shipeditordlg.cpp @@ -1250,11 +1250,9 @@ int CShipEditorDlg::update_data(int redraw) update_sexp_references(old_name, str); ai_update_goal_references(sexp_ref_type::SHIP, old_name, str); update_texture_replacements(old_name, str); - for (i=0; i= 0) + strcpy_s(Reinforcements[i].name, str); Update_window = 1; } diff --git a/fred2/shipflagsdlg.cpp b/fred2/shipflagsdlg.cpp index cddd3735945..c591c8620fc 100644 --- a/fred2/shipflagsdlg.cpp +++ b/fred2/shipflagsdlg.cpp @@ -233,13 +233,8 @@ BOOL ship_flags_dlg::OnInitDialog() m_respawn_priority.init(shipp->respawn_priority); } - for (j=0; jship_name)) { - break; - } - } - - reinforcement = (j < Num_reinforcements) ? 1 : 0; + j = find_item_with_string(Reinforcements, &reinforcements::name, shipp->ship_name); + reinforcement = (j >= 0) ? 1 : 0; // check if ship in wing ship_in_wing = (shipp->wingnum != -1);; @@ -299,12 +294,8 @@ BOOL ship_flags_dlg::OnInitDialog() m_respawn_priority.init(shipp->respawn_priority); } - for (j=0; jship_name)) { - break; - } - } - reinforcement = tristate_set(j < Num_reinforcements, reinforcement); + j = find_item_with_string(Reinforcements, &reinforcements::name, shipp->ship_name); + reinforcement = tristate_set(j >= 0, reinforcement); // check if ship in wing ship_in_wing = (shipp->wingnum != -1);; @@ -440,18 +431,7 @@ void ship_flags_dlg::update_ship(int shipnum) if (m_reinforcement.GetCheck() != 2) { - //Check if we're trying to add more and we've got too many. - if( (Num_reinforcements >= MAX_REINFORCEMENTS) && (m_reinforcement.GetCheck() == 1)) - { - char error_message[256]; - sprintf(error_message, "Too many reinforcements; could not add ship '%s' to reinforcement list!", shipp->ship_name); - MessageBox(error_message); - } - //Otherwise, just update as normal. - else - { - set_reinforcement(shipp->ship_name, m_reinforcement.GetCheck()); - } + set_reinforcement(shipp->ship_name, m_reinforcement.GetCheck()); } switch (m_cargo_known.GetCheck()) { diff --git a/fred2/wing.cpp b/fred2/wing.cpp index f1c06dcc027..37f8c7a05eb 100644 --- a/fred2/wing.cpp +++ b/fred2/wing.cpp @@ -303,11 +303,8 @@ int delete_wing(int wing_num, int bypass) { return r; already_deleting_wing = 1; - for (i = 0; iAddString(Ships[Wings[cur_wing].ship_index[i]].ship_name); m_threshold_spin.SetRange(0, static_cast(calc_max_wave_treshold())); - for (i=0; i= 0) m_reinforcement = TRUE; else m_reinforcement = FALSE; @@ -708,11 +706,9 @@ int wing_editor::update_data(int redraw) update_sexp_references(old_name, str); ai_update_goal_references(sexp_ref_type::WING, old_name, str); update_texture_replacements(old_name, str); - for (i=0; i= 0) + strcpy_s(Reinforcements[i].name, str); for (i=0; i= MAX_REINFORCEMENTS) && (m_reinforcement == 1)) - { - if (bypass_errors) - return 1; - - bypass_errors = 1; - - char error_message[256]; - sprintf(error_message, "Too many reinforcements; could not add wing '%s' to reinforcement list!", str); - MessageBox(error_message, "Error", MB_ICONEXCLAMATION | MB_OK); - - //clear the flag - m_reinforcement = 0; - UpdateData(FALSE); - - return -1; - - } - //Otherwise, just update as normal. - else if (set_reinforcement(str, m_reinforcement) == 1) + // Update as normal. + if (set_reinforcement(str, m_reinforcement) == 1) { free_sexp2(Wings[cur_wing].arrival_cue); Wings[cur_wing].arrival_cue = Locked_sexp_false; diff --git a/qtfred/src/mission/Editor.cpp b/qtfred/src/mission/Editor.cpp index 64e1d8d755c..a83cf54b82c 100644 --- a/qtfred/src/mission/Editor.cpp +++ b/qtfred/src/mission/Editor.cpp @@ -33,6 +33,7 @@ #include "starfield/starfield.h" // stars_init, stars_pre_level_init, stars_post_level_init #include "hud/hudsquadmsg.h" #include "globalincs/linklist.h" +#include "globalincs/utility.h" #include "ui/QtGraphicsOperations.h" @@ -251,7 +252,7 @@ void Editor::maybeUseAutosave(std::string& filepath) bool Editor::loadMission(const std::string& mission_name, int flags) { char name[512], * old_name; - int i, j, k, ob; + int i, j, ob; object* objp; // activate the localizer hash table @@ -387,11 +388,10 @@ bool Editor::loadMission(const std::string& mission_name, int flags) { update_sexp_references(old_name, name); ai_update_goal_references(sexp_ref_type::SHIP, old_name, name); update_texture_replacements(old_name, name); - for (k = 0; k < Num_reinforcements; k++) { - if (!strcmp(old_name, Reinforcements[k].name)) { - Assert(strlen(name) < NAME_LENGTH); - strcpy_s(Reinforcements[k].name, name); - } + int k = find_item_with_string(Reinforcements, &reinforcements::name, old_name); + if (k >= 0) { + Assert(strlen(name) < NAME_LENGTH); + strcpy_s(Reinforcements[k].name, name); } strcpy_s(Ships[Wings[i].ship_index[j]].ship_name, name); @@ -956,7 +956,7 @@ int Editor::getNumMarked() { } int Editor::dup_object(object* objp) { - int i, j, n, inst, obj = -1; + int i, n, inst, obj = -1; ai_info* aip1, * aip2; object* objp1, * objp2; ship_subsys* subp1, * subp2; @@ -1018,17 +1018,10 @@ int Editor::dup_object(object* objp) { subp2 = GET_NEXT(subp2); } - for (i = 0; i < Num_reinforcements; i++) { - if (!stricmp(Reinforcements[i].name, Ships[inst].ship_name)) { - if (Num_reinforcements < MAX_REINFORCEMENTS) { - j = Num_reinforcements++; - strcpy_s(Reinforcements[j].name, Ships[n].ship_name); - Reinforcements[j].type = Reinforcements[i].type; - Reinforcements[j].uses = Reinforcements[i].uses; - } - - break; - } + i = find_item_with_string(Reinforcements, &reinforcements::name, Ships[inst].ship_name); + if (i >= 0) { + Reinforcements.push_back(Reinforcements[i]); + strcpy_s(Reinforcements.back().name, Ships[n].ship_name); } } else if (objp->type == OBJ_WAYPOINT) { @@ -1184,12 +1177,7 @@ int Editor::common_object_delete(int obj) { invalidate_references(name, sexp_ref_type::SHIP); } - for (i = 0; i < Num_reinforcements; i++) { - if (!stricmp(name, Reinforcements[i].name)) { - delete_reinforcement(i); - break; - } - } + delete_reinforcement(name); // check if any ship is docked with this ship and break dock if so while (object_is_docked(&Objects[obj])) { @@ -1396,13 +1384,8 @@ int Editor::reference_handler(const char* name, sexp_ref_type type, int obj) { return 0; } - for (n = 0; n < Num_reinforcements; n++) { - if (!stricmp(name, Reinforcements[n].name)) { - break; - } - } - - if (n < Num_reinforcements) { + n = find_item_with_string(Reinforcements, &reinforcements::name, name); + if (n >= 0) { sprintf(msg, "Ship \"%s\" is a reinforcement unit.\n" "Do you want to delete it anyway?", name); @@ -1639,11 +1622,9 @@ int Editor::invalidate_references(const char* name, sexp_ref_type type) { update_sexp_references(name, new_name); ai_update_goal_references(type, name, new_name); update_texture_replacements(name, new_name); - for (i = 0; i < Num_reinforcements; i++) { - if (!stricmp(name, Reinforcements[i].name)) { - strcpy_s(Reinforcements[i].name, new_name); - } - } + i = find_item_with_string(Reinforcements, &reinforcements::name, name); + if (i >= 0) + strcpy_s(Reinforcements[i].name, new_name); return 0; } @@ -1670,8 +1651,6 @@ void Editor::update_texture_replacements(const char* old_name, const char* new_n } } int Editor::rename_ship(int ship, const char* name) { - int i; - Assert(ship >= 0); Assert(strlen(name) < NAME_LENGTH); @@ -1682,11 +1661,9 @@ int Editor::rename_ship(int ship, const char* name) { update_sexp_references(Ships[ship].ship_name, name); ai_update_goal_references(sexp_ref_type::SHIP, Ships[ship].ship_name, name); update_texture_replacements(Ships[ship].ship_name, name); - for (i = 0; i < Num_reinforcements; i++) { - if (!stricmp(Ships[ship].ship_name, Reinforcements[i].name)) { - strcpy_s(Reinforcements[i].name, name); - } - } + int i = find_item_with_string(Reinforcements, &reinforcements::name, Ships[ship].ship_name); + if (i >= 0) + strcpy_s(Reinforcements[i].name, name); strcpy_s(Ships[ship].ship_name, name); @@ -1694,14 +1671,12 @@ int Editor::rename_ship(int ship, const char* name) { return 0; } -void Editor::delete_reinforcement(int num) { - int i; - - for (i = num; i < Num_reinforcements - 1; i++) { - Reinforcements[i] = Reinforcements[i + 1]; - } +void Editor::delete_reinforcement(const char* name) { + int i = find_item_with_string(Reinforcements, &reinforcements::name, name); + if (i < 0) + return; - Num_reinforcements--; + Reinforcements.erase(Reinforcements.begin() + i); missionChanged(); } int Editor::check_wing_dependencies(int wing_num) { @@ -1709,17 +1684,11 @@ int Editor::check_wing_dependencies(int wing_num) { return reference_handler(name, sexp_ref_type::WING, -1); } int Editor::set_reinforcement(const char* name, int state) { - int i, index, cur = -1; - - for (i = 0; i < Num_reinforcements; i++) { - if (!stricmp(Reinforcements[i].name, name)) { - cur = i; - } - } + int index; + int cur = find_item_with_string(Reinforcements, &reinforcements::name, name); if (!state && (cur != -1)) { - Num_reinforcements--; - Reinforcements[cur] = Reinforcements[Num_reinforcements]; + Reinforcements.erase(Reinforcements.begin() + cur); // clear the ship/wing flag for this reinforcement index = ship_name_lookup(name); @@ -1739,14 +1708,15 @@ int Editor::set_reinforcement(const char* name, int state) { return -1; } - if (state && (cur == -1) && (Num_reinforcements < MAX_REINFORCEMENTS)) { + if (state && (cur == -1)) { Assert(strlen(name) < NAME_LENGTH); - strcpy_s(Reinforcements[Num_reinforcements].name, name); - Reinforcements[Num_reinforcements].uses = 1; - Reinforcements[Num_reinforcements].arrival_delay = 0; - memset(Reinforcements[Num_reinforcements].no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH); - memset(Reinforcements[Num_reinforcements].yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH); - Num_reinforcements++; + reinforcements reinforcement; + strcpy_s(reinforcement.name, name); + reinforcement.uses = 1; + reinforcement.arrival_delay = 0; + memset(reinforcement.no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH); + memset(reinforcement.yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH); + Reinforcements.push_back(std::move(reinforcement)); // set the reinforcement flag on the ship or wing index = ship_name_lookup(name); @@ -2652,21 +2622,17 @@ int Editor::global_error_check_impl() { } } - if (Num_reinforcements > MAX_REINFORCEMENTS) { - return internal_error("Number of reinforcements exceeds max limit"); - } - - for (i = 0; i < Num_reinforcements; i++) { + for (const auto &reinforcement : Reinforcements) { z = 0; for (ship = 0; ship < MAX_SHIPS; ship++) { - if ((Ships[ship].objnum >= 0) && !stricmp(Ships[ship].ship_name, Reinforcements[i].name)) { + if ((Ships[ship].objnum >= 0) && !stricmp(Ships[ship].ship_name, reinforcement.name)) { z = 1; break; } } for (wing = 0; wing < MAX_WINGS; wing++) { - if (Wings[wing].wave_count && !stricmp(Wings[wing].name, Reinforcements[i].name)) { + if (Wings[wing].wave_count && !stricmp(Wings[wing].name, reinforcement.name)) { z = 1; break; } diff --git a/qtfred/src/mission/Editor.h b/qtfred/src/mission/Editor.h index ecb005f913c..615b3c5b8ec 100644 --- a/qtfred/src/mission/Editor.h +++ b/qtfred/src/mission/Editor.h @@ -330,7 +330,7 @@ class Editor : public QObject { int invalidate_references(const char* name, sexp_ref_type type); - void delete_reinforcement(int num); + void delete_reinforcement(const char* name); // changes the currently selected wing. It is assumed that cur_wing == cur_ship's wing // number. Don't call this if this won't be true, or else you'll screw things up. diff --git a/qtfred/src/mission/EditorWing.cpp b/qtfred/src/mission/EditorWing.cpp index 38865c51adf..7410b7f83f7 100644 --- a/qtfred/src/mission/EditorWing.cpp +++ b/qtfred/src/mission/EditorWing.cpp @@ -4,6 +4,7 @@ #include "mission/dialogs/FormWingDialogModel.h" #include +#include #include namespace { @@ -29,12 +30,7 @@ int Editor::delete_wing(int wing_num, int bypass) } already_deleting_wing = 1; - for (i = 0; i < Num_reinforcements; i++) { - if (!stricmp(Wings[wing_num].name, Reinforcements[i].name)) { - delete_reinforcement(i); - break; - } - } + delete_reinforcement(Wings[wing_num].name); invalidate_references(Wings[wing_num].name, sexp_ref_type::WING); if (!bypass) { diff --git a/qtfred/src/mission/dialogs/ReinforcementsEditorDialogModel.cpp b/qtfred/src/mission/dialogs/ReinforcementsEditorDialogModel.cpp index 2fe97baf0c2..9744cfa54a4 100644 --- a/qtfred/src/mission/dialogs/ReinforcementsEditorDialogModel.cpp +++ b/qtfred/src/mission/dialogs/ReinforcementsEditorDialogModel.cpp @@ -12,8 +12,8 @@ ReinforcementsDialogModel::ReinforcementsDialogModel(QObject* parent, EditorView void ReinforcementsDialogModel::initializeData() { - for (int i = 0; i < Num_reinforcements; i++) { - _reinforcementList.emplace_back(Reinforcements[i].name, Reinforcements[i].uses, Reinforcements[i].arrival_delay); + for (const auto &r : Reinforcements) { + _reinforcementList.emplace_back(r.name, r.uses, r.arrival_delay); } // add the wings to the model's internal storage @@ -81,20 +81,17 @@ void ReinforcementsDialogModel::initializeData() bool ReinforcementsDialogModel::apply() { - Num_reinforcements = static_cast(_reinforcementList.size()); - - int i = 0; - // Properly set all reinforcement info. + Reinforcements.clear(); for (auto& modelReinforcement : _reinforcementList) { - strcpy_s(Reinforcements[i].name, std::get<0>(modelReinforcement).c_str()); - Reinforcements[i].uses = std::get<1>(modelReinforcement); - Reinforcements[i].arrival_delay = std::get<2>(modelReinforcement); - Reinforcements[i].type = 0; - memset( Reinforcements[i].no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH ); - memset( Reinforcements[i].yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH ); - - i++; + reinforcements reinforcement; + strcpy_s(reinforcement.name, std::get<0>(modelReinforcement).c_str()); + reinforcement.uses = std::get<1>(modelReinforcement); + reinforcement.arrival_delay = std::get<2>(modelReinforcement); + reinforcement.type = 0; + memset( reinforcement.no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH ); + memset( reinforcement.yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH ); + Reinforcements.push_back(std::move(reinforcement)); } _shipWingPool.clear(); @@ -124,11 +121,6 @@ void ReinforcementsDialogModel::addToReinforcements(const SCP_vector } } - while (_reinforcementList.size() > MAX_REINFORCEMENTS) { - _shipWingPool.push_back(std::get<0>(_reinforcementList.back())); - _reinforcementList.pop_back(); - } - set_modified(); } diff --git a/qtfred/src/mission/dialogs/ShipEditor/ShipEditorDialogModel.cpp b/qtfred/src/mission/dialogs/ShipEditor/ShipEditorDialogModel.cpp index ee598a313a9..fb819703302 100644 --- a/qtfred/src/mission/dialogs/ShipEditor/ShipEditorDialogModel.cpp +++ b/qtfred/src/mission/dialogs/ShipEditor/ShipEditorDialogModel.cpp @@ -16,6 +16,7 @@ #include "missioneditor/common.h" #include +#include #include #include #include @@ -810,11 +811,10 @@ void ShipEditorDialogModel::setShipName(const SCP_string& m_ship_name) update_sexp_references(old_name, Ships[single_ship].ship_name); _editor->ai_update_goal_references(sexp_ref_type::SHIP, old_name, Ships[single_ship].ship_name); _editor->update_texture_replacements(old_name, Ships[single_ship].ship_name); - for (int j = 0; j < Num_reinforcements; j++) { - if (!strcmp(old_name, Reinforcements[j].name)) { - Assert(strlen(Ships[single_ship].ship_name) < NAME_LENGTH); - strcpy_s(Reinforcements[j].name, Ships[single_ship].ship_name); - } + int j = find_item_with_string(Reinforcements, &reinforcements::name, old_name); + if (j >= 0) { + Assert(strlen(Ships[single_ship].ship_name) < NAME_LENGTH); + strcpy_s(Reinforcements[j].name, Ships[single_ship].ship_name); } }