Skip to content

Vectorize MsgItems - #7687

Open
Kestrellius wants to merge 8 commits into
scp-fs2open:masterfrom
Kestrellius:maxmsg
Open

Vectorize MsgItems#7687
Kestrellius wants to merge 8 commits into
scp-fs2open:masterfrom
Kestrellius:maxmsg

Conversation

@Kestrellius

Copy link
Copy Markdown
Contributor

Convert MsgItems from an array to a vector, and remove the associated limit value and counter.

MsgItems is dynamically rewritten at runtime, and previously used a counter value of -1 to indicate that the array was in a state suitable for rewriting in some circumstances. To replicate this behavior, MsgItems is now actually an std::optional<SCP_vector<mmode_item>>, and its nullopt state corresponds to -1.

@wookieejedi wookieejedi added cleanup A modification or rewrite of code to make it more understandable or easier to maintain. fix A fix for bugs, not-a-bugs, and/or regressions. HUD A feature or issue related to the HUD labels Aug 6, 2026
@wookieejedi wookieejedi added this to the Release 26.0.1 milestone Aug 6, 2026

@Goober5000 Goober5000 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need some rework. I sent a message on Discord

@Kestrellius

Copy link
Copy Markdown
Contributor Author

There is an existing behavior where if you input a "comms menu move up" command in the same frame as opening the comms menu, you end up with a Selected_menu_item of -2, and the selection pointer isn't visible. You have to hit "move down" twice in order to get it to appear at the first entry, or "move up" once and it'll wrap around. At present this behavior is replicated faithfully in this branch, but alternatively I could fix this bug(?) by just early-returning out of the selection-movement commands if Rebuild_MsgItems is true. (Or if MsgItems.size() is 0?)

@wookieejedi

Copy link
Copy Markdown
Member

I vote fixing the bug would be good, though you may also want to look at #7688. Possibly they could all be combined into one PR here?

@Goober5000

Goober5000 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

I'd recommend, instead, fixing the bug in #7688 and keeping this PR focused on the vectorization. Then this PR can be rebased on 7688, after merge, and inherit or accommodate the fix.

@wookieejedi

wookieejedi commented Aug 7, 2026

Copy link
Copy Markdown
Member

Ah yeah good point! Though I think merging this one first might work better, and then I can fully test all the cleanup and fixes in 7688 once this is merged :)

@wookieejedi

Copy link
Copy Markdown
Member

@Goober5000 is this one good to merge now or is more review needed? Note, once this is merged I'll address various other fixes in 7688

@Goober5000

Copy link
Copy Markdown
Contributor

@Goober5000 is this one good to merge now or is more review needed? Note, once this is merged I'll address various other fixes in 7688

No, there are several things that need to be addressed, but I haven't had time to respond. Will try to follow up ASAP.

@wookieejedi

Copy link
Copy Markdown
Member

Thanks, sounds good!

@Goober5000 Goober5000 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be necessary to handle config mode, because MsgItems is only valid during a mission. So, in hudsquadmsg.cpp:

  • Remove the active = -1 assignment, and enclosing if(), in HudGaugeSquadMessage::render
  • Then, 22 lines later, add this before the if() and change the if() to use item_visible:
bool item_visible = config
    ? !(Hide_main_rearm_items_in_comms_gauge && (i == TYPE_REPAIR_REARM_ITEM || i == TYPE_REPAIR_REARM_ABORT_ITEM))
    : (MsgItems[First_menu_item + i].active >= 0);

@Goober5000 Goober5000 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional comments. With these changes, every ternary MsgItems ? sz2i(MsgItems.size()) : -1 can become just sz2i(MsgItems.size()).

Comment thread code/hud/hudsquadmsg.cpp
Comment thread code/hud/hudsquadmsg.cpp
Comment thread code/hud/hudsquadmsg.h Outdated
Comment thread code/hud/hudsquadmsg.cpp Outdated
Comment thread code/hud/hudsquadmsg.cpp
renderString(x + Header_offsets[0], y + fl2i(Header_offsets[1] * scale), title, scale, config);
}

int msg_items_size = !Rebuild_MsgItems ? sz2i(MsgItems.size()) : -1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still a ternary; it can directly use the size now

Comment thread code/hud/hudsquadmsg.cpp
void hud_squadmsg_page_down()
{
if ( (First_menu_item + MAX_MENU_DISPLAY) < Num_menu_items ) {
if ( !Rebuild_MsgItems && (First_menu_item + MAX_MENU_DISPLAY) < sz2i(MsgItems.size()) ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Rebuild_MsgItems check is no longer needed here

"Number of comm orders in the mission. 0 if comm menu is closed")
{
return ade_set_args(L, "i", Num_menu_items);
return ade_set_args(L, "i", (!Rebuild_MsgItems ? sz2i(MsgItems.size()) : -1));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still uses the ternary


return ade_set_args(L, "s", MsgItems[current].text.c_str());
SCP_string message = "Invalid comm item!";
if (!Rebuild_MsgItems && sz2i(MsgItems.size()) > current) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebuild_MsgItems is no longer needed, and you can use in_bounds here

}

if (MsgItems[current].active > 0) {
if (!Rebuild_MsgItems && sz2i(MsgItems.size()) > current && MsgItems[current].active > 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

idx--;

if ((idx < 0) || idx >= MAX_MENU_ITEMS)
if ((idx < 0))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double parentheses here

SCP_string message = "Invalid comm item!";
if (!Rebuild_MsgItems && sz2i(MsgItems.size()) > current) {
message = MsgItems[current].text;
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray semicolon

Comment thread code/hud/hudsquadmsg.cpp

Num_menu_items++;

}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has a trailing tab

Comment thread code/hud/hudsquadmsg.cpp
}
MsgItems[i].active = 1; // assume active
MsgItems.push_back({0, 1, lua_cat_list[i - NUM_COMM_ORDER_TYPES]}); // assume active
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra whitespace here

Comment thread code/hud/hudsquadmsg.cpp

//Fuctions that allow selection of specific comms menu items with simple up/down/select buttons
void hud_squadmsg_selection_move_down() {
void hud_squadmsg_selection_move( bool up ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has a bug and a few infelicities, but it turns out you can replace the whole thing with this:

void hud_squadmsg_selection_move( bool up )
{
	if (Rebuild_MsgItems || MsgItems.empty())
		return;

	//Check if comms menu is up
	if (Player->flags & PLAYER_FLAGS_MSG_MODE)
	{
		Display_selector = true;

		//play scrolling sound and reset the comms window timeout timer, so the window doesn't disappear while we select our item
		gamesnd_play_iface(InterfaceSounds::SCROLL);
		Msg_mode_timestamp = _timestamp(DEFAULT_MSG_TIMEOUT);

		//The selection moves +/-1 through the whole menu, wrapping around at either end.
		//First_menu_item and Selected_menu_item are just the page and the offset within the page.
		int num_items = sz2i(MsgItems.size());
		int selected_index = First_menu_item + Selected_menu_item;
		selected_index = (selected_index + (up ? -1 : 1) + num_items) % num_items;

		First_menu_item = (selected_index / MAX_MENU_DISPLAY) * MAX_MENU_DISPLAY;
		Selected_menu_item = selected_index % MAX_MENU_DISPLAY;
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cleanup A modification or rewrite of code to make it more understandable or easier to maintain. fix A fix for bugs, not-a-bugs, and/or regressions. HUD A feature or issue related to the HUD

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants