Skip to content

Conversation

@JohnAFernandez
Copy link
Contributor

Vectorizes the fireball limit. Thanks to @asarium for some great suggestions. In draft until we get a proper stress test from @EatThePath

For #2945

@wookieejedi
Copy link
Member

Yessss

constexpr int INTITIAL_FIREBALL_SIZE = 256;

SCP_vector<fireball> Fireball_vec(INTITIAL_FIREBALL_SIZE);
SCP_vector<int> Unused_fireball_indices(INTITIAL_FIREBALL_SIZE);
Copy link
Member

Choose a reason for hiding this comment

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

Careful with the vector constructor call. It adds that many elements and does not only reserve as much. Since you already have the relevant code to reserve the required size below, you can just use the default constructor here.


num = obj->instance;
fb = &Fireballs[num];
// some temporary asserts until we're sure the new system works fine.
Copy link
Member

Choose a reason for hiding this comment

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

Why temporary? More consistency checks are always good 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good

}


if (n == static_cast<int>(Fireball_vec.size())) {
Copy link
Member

Choose a reason for hiding this comment

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

If you keep the fireball a pointer and just point that to the correct element around line 787 then you can avoid having that very specific check here.


extern fireball Fireballs[MAX_FIREBALLS];
extern int Num_fireballs;
extern SCP_vector<fireball> Fireball_vec;
Copy link
Member

Choose a reason for hiding this comment

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

The name should stay as Fireballs for consistency with the rest of the code. That _vec suffix isn't really necessary.

Comment on lines 727 to 732
int count = 0;
for (auto& current_fireball : Fireball_vec) {
if (current_fireball.objnum >= 0) {
count++;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Since this relies on the internal implementation of the fireballs, it should be moved to within the fireball module and only called here via a function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK this all sounds easy to do. Thanks for the review!

Copy link
Contributor Author

@JohnAFernandez JohnAFernandez Feb 21, 2021

Choose a reason for hiding this comment

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

Take another look if you can! Changes should be included.

@JohnAFernandez JohnAFernandez force-pushed the Vectorize-fireballs-maybe branch from 58c3348 to 98fa18b Compare February 21, 2021 01:46
@JohnAFernandez JohnAFernandez marked this pull request as ready for review February 21, 2021 01:56
{
int count = static_cast<int>(Fireballs.size() - Unused_fireball_indices.size());

if (count < 0) {
Copy link
Member

Choose a reason for hiding this comment

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

This is a surprisingly simple way of doing this. Maybe add a comment in this function to explain why this works to make understanding this a bit easier.

Also, be careful with your casting range checks. The way this currently works means that this will not be hit in the cases you want. Since both size calls return a size_t, you will get an underflow if Unused_fireball_indices is larger than Fireballs. If you do the casts individually then the result will be as you expect it.

Also, is this a case you would expect? I can't think of something where this could happen without a programming error in which case an assertion would be sufficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not expecting it, I just got paranoid. So you're right, I should make it an assert.

@JohnAFernandez JohnAFernandez added cleanup A modification or rewrite of code to make it more understandable or easier to maintain. enhancement A new feature or upgrade of an existing feature to add additional functionality. labels Feb 27, 2021
(Without the main PC, so really hoping I don't do any typos)
@JohnAFernandez JohnAFernandez requested a review from asarium March 3, 2021 05:48
@JohnAFernandez JohnAFernandez added this to the Release 21.2 milestone Mar 10, 2021
@JohnAFernandez JohnAFernandez merged commit 609ed4e into scp-fs2open:master Mar 17, 2021
@JohnAFernandez JohnAFernandez deleted the Vectorize-fireballs-maybe branch March 18, 2021 03:32
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. enhancement A new feature or upgrade of an existing feature to add additional functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants