Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix projectiles being added a frame behind where they are fired and beam weapons not able to hit #5767

Merged

Conversation

JonBooth78
Copy link
Contributor

@JonBooth78 JonBooth78 commented Feb 17, 2024

This makes combat impossible if travelling at speed

Fixes #5695 and Fixes #5741

Revert initial change that fixed adding items to the vector whilst iterating it but introduced the frame lag and instead fix in a simple way.

… makes combat impossible if travelling at speed

Revert inital change that fixed adding items to the vector whilst iterating it but introduced the frame lag and instead fix in a simple way.
@JonBooth78
Copy link
Contributor Author

If anyone is interested, my overly clever fix is here: 563d305

src/Space.cpp Outdated
// https:/pioneerspacesim/pioneer/issues/5695
//
// THIS IS A HACK/WORKAROUND until a more proper solution can be found
for (size_t i = 0, e = m_bodies.size(); i != e; ++i) {
Copy link
Member

Choose a reason for hiding this comment

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

As far as I can tell, this will retain the old behavior where only the bodies present at the start of iteration are actually ticked here. To iterate over new-this-frame bodies, the condition needs to be changed to i != m_bodies.size() - e is evaluated once at the start of the loop and if new bodies are added to the end they will not be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So... the original behavior used an

for (auto b : m_bodies )

Which expands to

for (auto i = m_bodies.begin(), auto e = m_bodies.end(), i != e; ++i ) { auto b = *i;

So I went for the same logic, which means that their static update wouldn't get called as I wanted to make the code behave as close as possible to how it did before, without the risk of having invalid iterators.

After our IRC Chat, I agree, we should check size() each iteration rather than store it at the start as I think for projectiles (which are often the things added in this loop) we probably want to run their update too.

This will, I think make beam weapons more deadly as they'll hit in the first frame and not the second, and so have not degraded their power at all (if I read it all correctly) :-)

@JonBooth78 JonBooth78 changed the title Fix projectiles being added a frame behind where they are fired Fix projectiles being added a frame behind where they are fired and beam weapons not able to hit Feb 17, 2024
Copy link
Member

@Web-eWorks Web-eWorks left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@Web-eWorks Web-eWorks merged commit 27d5b3d into pioneerspacesim:master Feb 17, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Laser weapons do not damage ships Projectiles/laser appear offset from the ship depending on speed
2 participants