Skip to content

Commit 02f3060

Browse files
committed
make the reinforcements collection dynamic
As requested by a FREDder who has a plausible situation where more than 32 reinforcements might be handy. :)
1 parent 5f6309b commit 02f3060

17 files changed

Lines changed: 191 additions & 298 deletions

‎code/hud/hudsquadmsg.cpp‎

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,26 +1723,26 @@ int hud_squadmsg_send_wing_command( int wingnum, int command, int send_message,
17231723
// return number of available reinforcements, 0 if none available
17241724
int hud_squadmsg_reinforcements_available(int team)
17251725
{
1726-
int i, count = 0;
1726+
int count = 0;
17271727

1728-
for (i = 0; i < Num_reinforcements; i++) {
1728+
for (const auto &reinforcement: Reinforcements) {
17291729
int wingnum;
17301730

17311731
// no more left
1732-
if ( Reinforcements[i].num_uses >= Reinforcements[i].uses ){
1732+
if (reinforcement.num_uses >= reinforcement.uses ){
17331733
continue;
17341734
}
17351735

17361736
// incorrect team
1737-
if ( team != ship_get_reinforcement_team(i) ){
1737+
if ( team != ship_get_reinforcement_team(reinforcement) ){
17381738
continue;
17391739
}
17401740

17411741
// check the arrival cue sexpression of the ship/wing of this reinforcement.
17421742
// Goober5000 - if it can't arrive, it doesn't count. This should check
17431743
// for SEXP_FALSE as well as SEXP_KNOWN_FALSE, otherwise you end up with
17441744
// a reinforcement menu containing no valid selections.
1745-
if ( (wingnum = wing_name_lookup(Reinforcements[i].name, 1)) != -1 ) {
1745+
if ( (wingnum = wing_name_lookup(reinforcement.name, 1)) != -1 ) {
17461746
Assert ( Wings[wingnum].arrival_cue >= 0 );
17471747
if ( Sexp_nodes[Wings[wingnum].arrival_cue].value == SEXP_FALSE
17481748
|| Sexp_nodes[Wings[wingnum].arrival_cue].value == SEXP_KNOWN_FALSE ){
@@ -1751,7 +1751,7 @@ int hud_squadmsg_reinforcements_available(int team)
17511751
} else {
17521752
p_object *p_objp;
17531753

1754-
p_objp = mission_parse_get_arrival_ship( Reinforcements[i].name );
1754+
p_objp = mission_parse_get_arrival_ship( reinforcement.name );
17551755
if ( p_objp != NULL ) {
17561756
if ( Sexp_nodes[p_objp->arrival_cue].value == SEXP_FALSE
17571757
|| Sexp_nodes[p_objp->arrival_cue].value == SEXP_KNOWN_FALSE ){
@@ -1973,23 +1973,20 @@ void hud_squadmsg_msg_all_fighters()
19731973

19741974
// called to actually bring in a reinforcement. For single player games, always gets called.
19751975
// for multiplayer games, always called on the server side. Clients should never get here
1976-
void hud_squadmsg_call_reinforcement(int reinforcement_num, int /*player_num*/)
1976+
void hud_squadmsg_call_reinforcement(reinforcements &reinforcement, int /*player_num*/)
19771977
{
19781978
int i, delay;
1979-
reinforcements *rp;
19801979
p_object *p_objp;
19811980

1982-
rp = &Reinforcements[reinforcement_num];
1983-
19841981
// safety net mainly for multiplayer servers in case some odd data desync occurs between
19851982
// server and clients
1986-
if ( MULTIPLAYER_MASTER && (rp->num_uses >= rp->uses) ) {
1983+
if ( MULTIPLAYER_MASTER && (reinforcement.num_uses >= reinforcement.uses) ) {
19871984
return;
19881985
}
19891986

19901987
// check to see if the reinforcement called was a wing.
19911988
for (i = 0; i < Num_wings; i++ ) {
1992-
if ( !stricmp(rp->name, Wings[i].name) ) {
1989+
if ( !stricmp(reinforcement.name, Wings[i].name) ) {
19931990
// if the wing is currently present, skip this request so we don't waste a "use"
19941991
if (Wings[i].current_count > 0) {
19951992
return;
@@ -2001,7 +1998,7 @@ void hud_squadmsg_call_reinforcement(int reinforcement_num, int /*player_num*/)
20011998
Wings[i].flags.set(Ship::Wing_Flags::Reset_reinforcement);
20021999

20032000
// set up the arrival delay. If it is 0, then make is some random number of seconds
2004-
delay = rp->arrival_delay;
2001+
delay = reinforcement.arrival_delay;
20052002
if ( delay == 0 )
20062003
delay = (int)(frand() * 3.0) + 3;
20072004
Wings[i].arrival_delay = timestamp(delay * 1000);
@@ -2012,14 +2009,14 @@ void hud_squadmsg_call_reinforcement(int reinforcement_num, int /*player_num*/)
20122009
// if we found no wing name that matched the reinforcement name, then look for a ship
20132010
// of the same name
20142011
if ( i == Num_wings ) {
2015-
p_objp = mission_parse_get_arrival_ship( rp->name );
2012+
p_objp = mission_parse_get_arrival_ship( reinforcement.name );
20162013
if ( p_objp ) {
20172014
// by resetting the reinforcement flag, we will allow code which normally handles arrivals
20182015
// to make this reinforcement arrive. Doing so keeps the data structures clean.
20192016
p_objp->flags.remove(Mission::Parse_Object_Flags::SF_Reinforcement);
20202017

20212018
// set up the arrival delay
2022-
delay = rp->arrival_delay;
2019+
delay = reinforcement.arrival_delay;
20232020
if ( delay == 0 )
20242021
delay = (int)(frand() * 3.0) + 3; // between 3 and 6 seconds to arrive
20252022
p_objp->arrival_delay = timestamp(delay * 1000);
@@ -2031,7 +2028,7 @@ void hud_squadmsg_call_reinforcement(int reinforcement_num, int /*player_num*/)
20312028

20322029
// increment the number of times this is used. Incremented here on single player and multiplayer
20332030
// server side only. Clients keep track of own count when they actually call something in.
2034-
rp->num_uses++;
2031+
reinforcement.num_uses++;
20352032

20362033
// commented out on 9/9/98 because these messages simply are not used
20372034
/*
@@ -2043,50 +2040,49 @@ void hud_squadmsg_call_reinforcement(int reinforcement_num, int /*player_num*/)
20432040
break;
20442041
20452042
//if ( i > 0 )
2046-
// message_send_to_player( rp->yes_messages[Random::next(i)], rp->name, MESSAGE_PRIORITY_NORMAL, HUD_SOURCE_FRIENDLY );
2043+
// message_send_to_player( rp->yes_messages[Random::next(i)], reinforcement.name, MESSAGE_PRIORITY_NORMAL, HUD_SOURCE_FRIENDLY );
20472044
*/
20482045

2049-
mission_log_add_entry(LOG_PLAYER_CALLED_FOR_REINFORCEMENT, rp->name, NULL);
2046+
mission_log_add_entry(LOG_PLAYER_CALLED_FOR_REINFORCEMENT, reinforcement.name, nullptr);
20502047
}
20512048

20522049
// function to display a list of reinforcements available to the player
20532050
void hud_squadmsg_reinforcement_select()
20542051
{
2055-
int i, k, wingnum;
2056-
reinforcements *rp;
2052+
int i = -1, wingnum;
20572053

20582054
if ( Num_menu_items == -1 ) {
20592055
Num_menu_items = 0;
2060-
for (i = 0; i < Num_reinforcements; i++) {
2061-
rp = &Reinforcements[i];
2062-
SCP_string rp_name = rp->name;
2056+
for (const auto &reinforcement: Reinforcements) {
2057+
++i; // start at 0; increment at top of loop due to continues
2058+
SCP_string r_name = reinforcement.name; // copy it
20632059

20642060
// don't put reinforcements onto the list that have already been used up.
2065-
if ( rp->num_uses >= rp->uses ){
2061+
if (reinforcement.num_uses >= reinforcement.uses){
20662062
continue;
20672063
}
20682064

20692065
// don't put items which are not on my team
2070-
if((Player_ship != NULL) && (ship_get_reinforcement_team(i) != Player_ship->team)){
2066+
if((Player_ship != NULL) && (ship_get_reinforcement_team(reinforcement) != Player_ship->team)){
20712067
continue;
20722068
}
20732069

20742070
// check the arrival cue sexpression of the ship/wing of this reinforcement.
20752071
// Goober5000 - if it can't arrive, it doesn't count. This should check
20762072
// for SEXP_FALSE as well as SEXP_KNOWN_FALSE, otherwise you end up with
20772073
// a reinforcement menu containing no valid selections.
2078-
if ( (wingnum = wing_name_lookup(rp->name, 1)) != -1 ) {
2074+
if ( (wingnum = wing_name_lookup(reinforcement.name, 1)) != -1 ) {
20792075
Assert ( Wings[wingnum].arrival_cue >= 0 );
20802076
if ( Sexp_nodes[Wings[wingnum].arrival_cue].value == SEXP_FALSE
20812077
|| Sexp_nodes[Wings[wingnum].arrival_cue].value == SEXP_KNOWN_FALSE ){
20822078
continue;
20832079
}
20842080

2085-
end_string_at_first_hash_symbol(rp_name);
2081+
end_string_at_first_hash_symbol(r_name);
20862082
} else {
20872083
p_object *p_objp;
20882084

2089-
p_objp = mission_parse_get_arrival_ship( rp->name );
2085+
p_objp = mission_parse_get_arrival_ship(reinforcement.name );
20902086
if ( p_objp != NULL ) {
20912087
if ( Sexp_nodes[p_objp->arrival_cue].value == SEXP_FALSE
20922088
|| Sexp_nodes[p_objp->arrival_cue].value == SEXP_KNOWN_FALSE ){
@@ -2097,15 +2093,15 @@ void hud_squadmsg_reinforcement_select()
20972093
continue;
20982094
}
20992095

2100-
rp_name = p_objp->get_display_name(); // this will handle getting rid of the hash if necessary
2096+
r_name = p_objp->get_display_name(); // this will handle getting rid of the hash if necessary
21012097
}
21022098

21032099
Assert ( Num_menu_items < MAX_MENU_ITEMS );
2104-
MsgItems[Num_menu_items].text = std::move(rp_name);
2100+
MsgItems[Num_menu_items].text = std::move(r_name);
21052101
MsgItems[Num_menu_items].instance = i;
21062102
MsgItems[Num_menu_items].active = 0;
21072103

2108-
if ( rp->flags & RF_IS_AVAILABLE ) {
2104+
if ( reinforcement.flags & RF_IS_AVAILABLE ) {
21092105
MsgItems[Num_menu_items].active = 1;
21102106
}
21112107

@@ -2115,7 +2111,7 @@ void hud_squadmsg_reinforcement_select()
21152111

21162112
// hud_squadmsg_display_menu( "Select Reinforcement" );
21172113
strcpy_s(Squad_msg_title, XSTR( "Select Ship/Wing", 319)); // AL 11-14-97: Reinforcement didn't fit, so using this for now
2118-
k = hud_squadmsg_get_key();
2114+
int k = hud_squadmsg_get_key();
21192115
if (k != -1) {
21202116
int rnum;
21212117

@@ -2135,7 +2131,7 @@ void hud_squadmsg_reinforcement_select()
21352131
Reinforcements[rnum].num_uses++; // increment this variable here since clients need to maintain a valid count
21362132
send_player_order_packet(SQUAD_MSG_REINFORCEMENT, rnum, 0);
21372133
} else {
2138-
hud_squadmsg_call_reinforcement(rnum);
2134+
hud_squadmsg_call_reinforcement(Reinforcements[rnum]);
21392135
}
21402136
}
21412137
}

‎code/hud/hudsquadmsg.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define MESSAGE_ALL_FIGHTERS -999
3131

3232
class object;
33+
struct reinforcements;
3334

3435
// defines for messages that can be sent from the player. Indexes into Player_orders
3536

@@ -181,7 +182,7 @@ extern void hud_squadmsg_rearm_shortcut();
181182
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 );
182183
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 );
183184
extern void hud_squadmsg_send_to_all_fighters( int command, int player_num = -1 );
184-
extern void hud_squadmsg_call_reinforcement(int reinforcement_num, int player_num = -1);
185+
extern void hud_squadmsg_call_reinforcement(reinforcements &reinforcement, int player_num = -1);
185186

186187
extern int hud_squadmsg_reinforcements_available(int team);
187188

‎code/mission/missionparse.cpp‎

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5995,60 +5995,58 @@ void parse_messages(mission *pm, int flags)
59955995

59965996
void parse_reinforcement(mission *pm)
59975997
{
5998-
reinforcements *ptr;
5998+
reinforcements reinforcement;
59995999
p_object *rforce_obj = NULL;
60006000
int instance = -1;
60016001

6002-
Assert(Num_reinforcements < MAX_REINFORCEMENTS);
60036002
Assert(pm != NULL);
6004-
ptr = &Reinforcements[Num_reinforcements];
60056003

60066004
required_string("$Name:");
6007-
stuff_string(ptr->name, F_NAME, NAME_LENGTH);
6005+
stuff_string(reinforcement.name, F_NAME, NAME_LENGTH);
60086006

6009-
find_and_stuff("$Type:", &ptr->type, F_NAME, Reinforcement_type_names, Num_reinforcement_type_names, "reinforcement type");
6007+
find_and_stuff("$Type:", &reinforcement.type, F_NAME, Reinforcement_type_names, Num_reinforcement_type_names, "reinforcement type");
60106008

60116009
required_string("$Num times:");
6012-
stuff_int(&ptr->uses);
6013-
ptr->num_uses = 0;
6010+
stuff_int(&reinforcement.uses);
6011+
reinforcement.num_uses = 0;
60146012

60156013
// reset the flags to 0
6016-
ptr->flags = 0;
6014+
reinforcement.flags = 0;
60176015

60186016
if ( optional_string("+Arrival delay:") )
60196017
{
60206018
int delay;
60216019
stuff_int(&delay);
60226020
if (delay < 0)
60236021
{
6024-
Warning(LOCATION, "Cannot have arrival delay < 0 on reinforcement %s", ptr->name);
6022+
Warning(LOCATION, "Cannot have arrival delay < 0 on reinforcement %s", reinforcement.name);
60256023
delay = 0;
60266024
}
60276025

6028-
ptr->arrival_delay = delay;
6026+
reinforcement.arrival_delay = delay;
60296027
}
60306028

60316029
if ( optional_string("+No Messages:") ){
6032-
stuff_string_list( ptr->no_messages, MAX_REINFORCEMENT_MESSAGES );
6030+
stuff_string_list( reinforcement.no_messages, MAX_REINFORCEMENT_MESSAGES );
60336031
}
60346032

60356033
if ( optional_string("+Yes Messages:") ){
6036-
stuff_string_list( ptr->yes_messages, MAX_REINFORCEMENT_MESSAGES );
6034+
stuff_string_list( reinforcement.yes_messages, MAX_REINFORCEMENT_MESSAGES );
60376035
}
60386036

60396037
// sanity check on the names of reinforcements
6040-
rforce_obj = mission_parse_find_parse_object(ptr->name);
6038+
rforce_obj = mission_parse_find_parse_object(reinforcement.name);
60416039

60426040
if (rforce_obj == NULL) {
6043-
if ((instance = wing_name_lookup(ptr->name, 1)) == -1) {
6044-
Warning(LOCATION, "Reinforcement %s not found as ship or wing", ptr->name);
6041+
if ((instance = wing_name_lookup(reinforcement.name, 1)) == -1) {
6042+
Warning(LOCATION, "Reinforcement %s not found as ship or wing", reinforcement.name);
60456043
return;
60466044
}
60476045
} else {
60486046
// Individual ships in wings can't be reinforcements - FUBAR
60496047
if (rforce_obj->wingnum >= 0)
60506048
{
6051-
Warning(LOCATION, "Reinforcement %s is part of a wing - Ignoring reinforcement declaration", ptr->name);
6049+
Warning(LOCATION, "Reinforcement %s is part of a wing - Ignoring reinforcement declaration", reinforcement.name);
60526050
return;
60536051
}
60546052
else
@@ -6060,10 +6058,10 @@ void parse_reinforcement(mission *pm)
60606058
// now, if the reinforcement is a wing, then set the number of waves of the wing == number of
60616059
// uses of the reinforcement
60626060
if (instance >= 0) {
6063-
Wings[instance].num_waves = ptr->uses;
6061+
Wings[instance].num_waves = reinforcement.uses;
60646062
}
60656063

6066-
Num_reinforcements++;
6064+
Reinforcements.push_back(std::move(reinforcement));
60676065
}
60686066

60696067
void parse_reinforcements(mission *pm)
@@ -7305,7 +7303,7 @@ void mission_init(mission *pm, bool quick_init)
73057303
for (int i = 0; i < MAX_WINGS; i++)
73067304
Wings[i].clear();
73077305

7308-
Num_reinforcements = 0;
7306+
Reinforcements.clear();
73097307

73107308
Parse_props.clear();
73117309

@@ -8055,27 +8053,24 @@ int mission_set_arrival_location(anchor_t anchor, ArrivalLocation location, int
80558053
/**
80568054
* Mark a reinforcement as available
80578055
*/
8058-
void mission_parse_mark_reinforcement_available(char *name)
8056+
void mission_parse_mark_reinforcement_available(const char *name)
80598057
{
8060-
int i;
8061-
reinforcements *rp;
8062-
8063-
for (i = 0; i < Num_reinforcements; i++) {
8064-
rp = &Reinforcements[i];
8065-
if ( !stricmp(rp->name, name) ) {
8066-
if ( !(rp->flags & RF_IS_AVAILABLE) ) {
8067-
rp->flags |= RF_IS_AVAILABLE;
8058+
int i = find_item_with_string(Reinforcements, &reinforcements::name, name);
8059+
if (i >= 0)
8060+
{
8061+
auto &r = Reinforcements[i];
8062+
if (!(r.flags & RF_IS_AVAILABLE))
8063+
{
8064+
r.flags |= RF_IS_AVAILABLE;
80688065

8069-
// tell all of the clients.
8070-
if ( MULTIPLAYER_MASTER ) {
8071-
send_reinforcement_avail( i );
8072-
}
8073-
}
8074-
return;
8066+
// tell all of the clients.
8067+
if (MULTIPLAYER_MASTER)
8068+
send_reinforcement_avail(i);
80758069
}
8070+
return;
80768071
}
80778072

8078-
Assert ( i < Num_reinforcements );
8073+
Assertion(false, "Reinforcement '%s' not found!", name);
80798074
}
80808075

80818076
/**
@@ -8101,12 +8096,9 @@ int mission_did_ship_arrive(p_object *objp, bool force_arrival)
81018096

81028097
// if we're forcing the arrival, then "use" the reinforcement; otherwise don't process anything else
81038098
if (force_arrival) {
8104-
for (int i = 0; i < Num_reinforcements; i++) {
8105-
auto rp = &Reinforcements[i];
8106-
if (!stricmp(rp->name, objp->name)) {
8107-
rp->num_uses++;
8108-
break;
8109-
}
8099+
int i = find_item_with_string(Reinforcements, &reinforcements::name, objp->name);
8100+
if (i >= 0) {
8101+
Reinforcements[i].num_uses++;
81108102
}
81118103
} else {
81128104
return -1;
@@ -8358,12 +8350,9 @@ bool mission_maybe_make_wing_arrive(int wingnum, bool force_arrival)
83588350

83598351
// if we're forcing the arrival, then "use" the reinforcement; otherwise don't process anything else
83608352
if (force_arrival && wingp->current_count == 0) {
8361-
for (int i = 0; i < Num_reinforcements; i++) {
8362-
auto rp = &Reinforcements[i];
8363-
if (!stricmp(rp->name, wingp->name)) {
8364-
rp->num_uses++;
8365-
break;
8366-
}
8353+
int i = find_item_with_string(Reinforcements, &reinforcements::name, wingp->name);
8354+
if (i >= 0) {
8355+
Reinforcements[i].num_uses++;
83678356
}
83688357
} else {
83698358
// reinforcement wings skip the rest of the function

0 commit comments

Comments
 (0)