Skip to content

Add set-guard-range sexp - #7251

Merged
wookieejedi merged 3 commits into
scp-fs2open:masterfrom
MjnMixael:set_guard_range
May 11, 2026
Merged

Add set-guard-range sexp#7251
wookieejedi merged 3 commits into
scp-fs2open:masterfrom
MjnMixael:set_guard_range

Conversation

@MjnMixael

Copy link
Copy Markdown
Contributor

I spent some time looking in to #7025 and it's actually a touch more complex than expected. The AI code limits the guard range with various hardcoded multipliers based on the offending object (weapon, hostile ship, asteroid, etc) and in one instance has an actual hardcoded distance in meters. These values also take in to account the size of the guardee. So it's really pretty complex.

I felt a Guard Tight order would further muddy this up by adding more hardcoded values or multipliers and it wouldn't be easily tunable or readily apparent what the actual difference is for players or mission designers.

After some experimentation I landed on a design that is maybe a touch overly simplistic in some ways but also more configurable. I've added a set-guard-range sexp that works as a clamp for the guard order ranges. The game will use the normal hardcoded logic but if the guard range is > 0 for the guardee ship then it will clamp the calculated range to the guard range. This can be done per-ship which gives mission designers flexibility to tune the guard range based on context. There is not any checking for positive values so you could probably get some pretty interesting AI behavior if the range is set to a value that's too small for the guardee ship class (50m for an Orion might be strange). However, I felt that adding a bunch of dynamic checks was overkill here and mission designers are smart enough to set realistic ranges.

This setup has the advantage of allowing the opposite as well. With set-guard-range you could expand the guard distance if the mission design calls for that sort of thing. Bonus!

Fixes #7025

@wookieejedi wookieejedi added enhancement A new feature or upgrade of an existing feature to add additional functionality. ai A feature or issue related to the AI algorithms labels Mar 2, 2026
Comment thread code/ai/aicode.cpp Outdated
@MjnMixael MjnMixael added this to the Release 26.0 milestone Mar 16, 2026
Comment thread code/ai/aicode.cpp
@wookieejedi

Copy link
Copy Markdown
Member

I realized I should have asked this here just to save any other reviewers the same question:
With the math on the PR looking good I was now looking at where other places that MAX_GUARD_DIST is used, and another are 4 other spots (2 sets of 2 uses). I'm guessing you likely looked at those already, but if not would it make sense to use ai_guard_threshold( there or perhaps not?

@MjnMixael

Copy link
Copy Markdown
Contributor Author

I realized I should have asked this here just to save any other reviewers the same question: With the math on the PR looking good I was now looking at where other places that MAX_GUARD_DIST is used, and another are 4 other spots (2 sets of 2 uses). I'm guessing you likely looked at those already, but if not would it make sense to use ai_guard_threshold( there or perhaps not?

I honestly can't remember why I chose to leave those out. I'll need to do some additional testing. I'm going to set this to draft until I can verify.

@MjnMixael

Copy link
Copy Markdown
Contributor Author

I realized I should have asked this here just to save any other reviewers the same question: With the math on the PR looking good I was now looking at where other places that MAX_GUARD_DIST is used, and another are 4 other spots (2 sets of 2 uses). I'm guessing you likely looked at those already, but if not would it make sense to use ai_guard_threshold( there or perhaps not?

I honestly can't remember why I chose to leave those out. I'll need to do some additional testing. I'm going to set this to draft until I can verify.

Ok, I remember why I left those untouched. Those places seem to be more about the guarding ship's orbital position rather than the distance at which threats would be intercepted. I didn't think that guard range should really mess with the orbital math or that might cause funny collisions and things. I'm still happy with where this is at.

@MjnMixael
MjnMixael marked this pull request as ready for review May 10, 2026 03:43
@wookieejedi

Copy link
Copy Markdown
Member

Sounds good to me and makes sense! That last thing that I wanted to ask about, in FotG and other mods it will be useful to have the ability to set this via script (so it's more optimized then running runSEXP for every new ship in a mission), or alternatively setting the default via ai_profiles. What do you think?

@MjnMixael

Copy link
Copy Markdown
Contributor Author

Sounds good to me and makes sense! That last thing that I wanted to ask about, in FotG and other mods it will be useful to have the ability to set this via script (so it's more optimized then running runSEXP for every new ship in a mission), or alternatively setting the default via ai_profiles. What do you think?

Good asks. Honestly I'm very focused on other PRs right now. Those would be good for follow ups.

@wookieejedi
wookieejedi merged commit 76c00db into scp-fs2open:master May 11, 2026
20 checks passed
@MjnMixael
MjnMixael deleted the set_guard_range branch May 11, 2026 14:48
BMagnu pushed a commit that referenced this pull request May 18, 2026
Follow-up to #7251, which added `set-guard-range`
This PR exposes that value to scripting, so scripters do not have to use the more expensive `runSEXP` to use it.

The value is initialized as -1 (`max_guard_radius = -1.0f;` line 7913 in ship.cpp), hence the default value for the script is -1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai A feature or issue related to the AI algorithms 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.

Guard Tight option needed

2 participants