Commit Graph
772 Commits
Author SHA1 Message Date
Goober5000andClaude Opus 4.8 bed228d264 avoid undefined behavior from UNREACHABLE on reachable code paths (#7555)
* avoid undefined behavior from UNREACHABLE on reachable code paths

UNREACHABLE expands to __assume(false) / __builtin_unreachable() in
release builds, which is undefined behavior if the code is ever actually
reached. Many UNREACHABLE sites only guard against unexpected-but-
possible runtime states (values from tables, mission/save files, network
packets, scripting, or user input), so reaching them should degrade
gracefully rather than invoke UB.

Replace UNREACHABLE with Assertion(false, ...) at all such sites. It
still pops an error in debug builds, but in release it is a harmless
no-op that simply falls through to the following code. Sites that are
genuinely unreachable by local invariants (e.g. a switch over a bounded
random number) are left as UNREACHABLE.

A few converted sites had no safe fall-through -- an uninitialized read,
null dereference, or null call would have followed -- so they also get a
minimal recovery: initialize the value, guard the dereference, return
early, or skip the offending item.

Where the converted site was a simple guard of the form
    if (x) { Assertion(false, ...); return; }
hoist the check onto the invariant instead:
    Assertion(!x, ...);
    if (x) { return; }
This states the expectation directly and shows the offending value in
the debug popup, while keeping the same graceful runtime degradation.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* addendum

After consultation with Lafiel, use UNREACHABLE in situations that are effectively impossible even if not provably impossible.  UNREACHABLE = "this shouldn't ever happen"; Assertion = "if this ever happens, it's a coding error".  Also fix a few other sites and change a couple to Warnings.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
2026-07-09 11:20:25 +09:00
Goober5000andClaude Fable 5 9ea9f51ce1 fix big ship waypoint completion (the "spinning Orion" bug)
Since FS1 retail, a large ship could get permanently stuck circling its
final waypoint.  Waypoint completion requires the ship center to pass
within MIN_DIST_TO_WAYPOINT_GOAL + sqrt(radius) of the point (~37m for
a 1000m-radius capital ship), and the check only runs when the ship
covers 0.1m in a single frame (a framerate-dependent minimum speed).
At any speed that passes that gate, a capital ship turning at
2PI/(3*rotation_time) has a turning circle of hundreds of meters, so
once it overshoots a misaligned final leg it can never re-aim into the
completion bubble: an endless overshoot-turnaround-miss loop.

Add ai_profiles flag "$fix big ship waypoint completion:"
(Fix_big_ship_waypoint_completion), enabled by default for mods
targeting 26.2.  With the flag set:

- big/huge ships complete a waypoint anywhere within their own radius
  of it (matching what the docking path code has always done), while
  small ships keep the retail sqrt(radius) tolerance; and
- the completion check runs every frame instead of requiring 0.1m of
  travel per frame, removing the framerate-dependent minimum speed
  (which at 240fps is 24 m/s, enough to affect fighters too).

With the flag unset, behavior is unchanged from retail.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-08 00:48:24 -04:00
the-eandClaude Sonnet 5 90f29236c1 Fix crash in ai_manage_bay_doors from stale destroyed parent ship
If the carrier a fighter launched from was destroyed earlier in the
mission, its Ships[] entry retains stale bay door state while its
object and model instance are already freed (objnum/model_instance_num
== -1). When the fighter later departs, ai_manage_bay_doors would still
try to animate the dead carrier's bay doors, calling
model_get_instance(-1) and asserting/crashing.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-07-05 12:08:48 +02:00
Mike Nelson f4d84ec47a Support Rearm Pool, Limits, and Other Features (#7249)
* framework for support ship rearm pool and flags

* scripting api

* FRED support

* QtFRED support

* do not save non-player-allowed weapons

* tvt and mission versioning

* fix variable name

* minor fixes and clarity

* oops

* add qtfred help docs

* fixes and cleanup

* clang and readability

* address feedback

* address feedback
2026-06-05 11:33:19 +02:00
Mike Nelson 31f77e5a32 Mines weapon type & weapon proximity triggers (#7461)
* new mine weapon type

* some fixes and cleanup

* fix some parsing issues

* make sure mines get plotted

* make mine spawned objects can get a correct target

* some cleanup and additional features

* chasing mines

* some Lua tweaks

* generalize proximity detonations

* minor tweaks

* collapse namespace

* address feedback
2026-06-05 14:06:00 +09:00
Mike Nelson 5a2ffd9f9c alt2 path mode with shortcutting (#7322) 2026-05-20 03:21:44 +02:00
Mike Nelson 76c00dbc06 Add set-guard-range sexp (#7251)
* set-guard-range

* make sure old path remains unchanged

* address feedback again
2026-05-11 07:53:58 -04:00
Mike Nelson 09029030c2 Qtfred unify flag lists (#7384)
* convert wing flags to the flags widget

* set tooltips on flags using flag descriptions

* add missing descriptions

* clang
2026-04-20 07:52:24 -04:00
BMagnu 1f5525e04f Fix virtual POF crashes (#7327)
* Ensure model data isn't deleted twice

* shared_ptr-ify model data

* fix virtual pof filename issue

* Fix tests

* remove redundant memsets

* Clang Tid
2026-03-31 10:02:24 -04:00
Goober5000 33d5f7ed3c refactor ship anchors
Testing in FotG exposed a bug that dates back to retail.  During mission parsing, arrival and departure anchors were represented by indexes into `Parse_names`.  After parsing, they were converted to ship indexes -- but only in FRED.  Since some functions are shared between FRED and FS2, this can produce inconsistent results, which can be hard to detect since parse indexes and ship indexes are usually the same.

This refactors arrival and departure anchors to be represented by ship registry indexes in all cases.  It also adds conversion functions for the benefit of FRED and QtFRED since those dialogs were designed with ship indexes in mind.
2026-03-06 22:00:32 -05:00
LuytenKy 1b8f920529 Implement a new ai_profiles.tbl field that allows the maximum number of allies allowed to be rearming to be modified (#7065) 2025-10-11 11:41:53 -04:00
Goober5000 7f8674c532 enhance goals with additional parameters 2025-09-18 21:30:43 -04:00
wookieejedi d42789decd Add new option for 'better combat collision avoidance' (#7036)
The `$better combat collision avoidance for fightercraft:` flag is incredibly useful in preventing AI from hitting large ships while attacking, and the ability to tune the collision avoidance aggression factor further helps mods tune behavior based on their ship speeds (tuning that value is via `+combat collision avoidance aggression for fightercraft:` ). Nevertheless, the improve collision avoidance still is not performed for that actual fightercraft's actual target, so there can be situations where a fast fighter attacks a large ship with high speed, and then when that fighter break off from the attack they turn directly into the large ship's hull. Allowing the target to be incorporated into the `better_collision_avoidance_triggered` check prevents this from happening. To allow the target to be a part of the checks we just simply need to not pass an 'ignor_ship` argument to `maybe_avoid_big_ship`.

This PR adds that ability via flag. This new flag is tested and works as expected. For example, without the flag a squadron of TIEs in FotG would literally ram themselves to death attacking some of our larger capital ships because they would break off attack too late, but with this flag they no longer accidentally collide at all. In discussion with Asteroth it was decided to make this enhanced behavior behind a flag, too.

This PR also fixes a small oversight from the original #2810 where `next_check_time` was never actually used within the `else` block of `maybe_avoid_big_ship`.
2025-09-15 07:31:02 -04:00
wookieejedi 2820532ab3 Expose value of idle circling with 'attack-any' (#7011)
* Expose value of idle circling with 'attack-any'

The attack-any order is extremely useful and utilized order, and there was one aspect that was ripe for exposure to modders: the `CHASE_CIRCLE_DIST` that was used to determine how big of a circle to fly around while the AI waited for new hostiles to arrive. For ships with high speeds, like FotG and others, the default radius of 200 m was a bit on the small side, which lead to ships flying in tight circles. This was was counterproductive to goals of tuning the AI to have a more cinematic style, such as where AI would fly around in large distances while waiting for enemies to arrive. Fortunately, this value is easy to expose and allow modders to tune with just a new ai_profiles value.

Tested and works as expected.

* improve name and use correct distance
2025-09-02 22:55:03 -04:00
Mike Nelson 966ac4d878 Allow testing bay paths in the lab (#6902)
* allow testing bay paths in the lab

* unused param

* static member methods
2025-08-13 08:36:53 -04:00
LuytenKyandwookieejedi b8c3c560d2 Implementation for an AI order to chase ship types -Second Attempt- (#6828)
* Back in business hopefully

* Refactor

* Resolve CI issue in aigoals.cpp

* Updating order.cpp

* Whoops

* Updated objecttypes.tbl to incorporate attack ship type

* Fix clang-tidy issue

* Forgot one other instance

---------

Co-authored-by: wookieejedi <[email protected]>
2025-08-07 09:56:18 -04:00
Mike Nelson e5a5c30fbf misc lab fixes 2025-08-04 14:24:03 -05:00
wookieejedi d3db542640 Expand collision avoidance to strafing (#6789)
Currently, the `$better combat collision avoidance for fightercraft:` flag only checks for possible big ship collisions when in normal chase mode (`AIM_CHASE`), but strafing a big ship is a separate AI mode (`AIM_STRAFE`). Per discussion with Asteroth, strafe attacking should also check for better collision avoidance, as the modder likely would expect that to fall under this AI flag. Also took the chance to make the collision avoidance check into a function to improve maintenance and usability.
2025-06-24 23:01:47 -04:00
John Fernandez a9664ff22f Merge pull request #6752 from Goober5000/stay_near_target_fix
fix STAY_NEAR_TARGET_ITEM
2025-06-19 19:44:55 -04:00
Mike Nelson 4ad015881d Lab dock tester (#6715)
* Add docking and undocking test to the Lab

* address feedback
2025-06-12 14:46:20 -04:00
Goober5000 3783cb0d1d fix STAY_NEAR_TARGET_ITEM
1. Allow "Stay near me" and "Stay near my target" to work even if the teams do not match
2. Allow "Stay near my target" to be issued regardless of team
3. Avoid divide-by-zero errors when running `ai_stay_near()`
2025-06-07 00:06:19 -04:00
Asteroth 7502cf37da Stationary jump types should not try to avoid collision (#6738)
* no sweeper jump collision avoidance

* few adjustments

* only use short range distance check
2025-06-02 21:57:41 -04:00
wookieejedi 78d2e663d7 One fix and one enhancement to AI shield behavior (#6737)
* One fix and one enhancement to AI shield behavior

This PR adds one fix and one enhancement to AI shield behavior.

1) Fix: Testing, especially in FotG, revealed an edge-case bug with shield management where the AI will not direct shield energy to a quadrant if it was hit when that quadrant was down. In other words, if a shield quadrant is knocked down to 0 and continues to be shot the AI will not realize it needs to direct energy to that quadrant. This is because `quadrant` is set to -1 if a shield segment is down. This ensures that the resulting effects and damage calculations properly ignore that quadrant downstream, but it also results in `aip->last_hit_quadrant` being set to -1. This PR adds a fix to properly record which shield was actually hit, so that the AI knows to direct energy to that quadrant.

2) Enhancement: In combat situations with fire coming from many directions, it is good practice to keep shields equalized, so this PR adds a flag for the AI to always attempt to balance shields, even if it is being fired upon (instead of the current behavior to always direct energy to the hit shield quadrant).

Both flags are tested and work as expected.

* fix copy paste

* rename to avoid previous confusion

* also make as ai class flag
2025-05-30 21:24:01 -04:00
Goober5000 9ea44f0479 ETS updates
Two updates to the energy transfer system.
1. Previously, for fighters missing a system (e.g. shields), the energy would be treated as if all energy from that system was routed to the other systems.  So, for example, a fighter's typical speed would be faster without shields than with shields.  A new AI profile flag now treats the other systems as if the energy had not been transferred.
2. At some point in development, Volition intended the `$Power Output` ships.tbl field to affect ETS.  A new AI profile flag now treats the power output as a factor of energy applied to energy systems.
2025-05-14 01:33:28 -04:00
Goober5000 a294559ee2 improve scripting support for waypoint lists
1. Add an explicit `mn.createWaypointList` function
2. Allow `mn.createWaypoint` to be used with an empty waypoint list
3. Update documentation of `mn.createWaypoint` to clarify optional arguments
2025-05-10 23:36:22 -04:00
Mike Nelson 36d6fbe7e5 Lab firing upgrades (#6703)
* Fix some issues preventing various secondary types from firing in the lab

* Add ability to fire turrets from the lab

* const char*, mjn please

* more cleanup

* range-based for loop

* Update comment
2025-05-01 09:50:27 -04:00
Goober5000 86f75c5d17 fix normalized vector handling (#6650)
Although normalized vectors in FSO are supposed to stay normalized, occasionally small floating point errors can pop up.  In most cases these do not cause a problem, but in certain cases these errors can rapidly accumulate and corrupt an otherwise well-behaved matrix.  This can most easily be seen with daisy-chained dock sequences or docked ships with angled dockpoints.

This PR audits the usage of `vm_vector_2_matrix_norm` to re-normalize vectors in cases where floating point errors could accumulate.  There are also a few new Assertions for extra vigilance.

Follow-up to #6596.  Fixes #6634.
2025-03-28 23:54:51 +01:00
Goober5000 4797ce55a2 use vm_vector_2_matrix_norm on normalized vectors
Most places in the code that use `vm_vector_2_matrix` do so on already-normalized vectors.  Changing these to `vm_vector_2_matrix_norm` avoids redundant normalization.
2025-03-02 23:07:09 -05:00
wookieejedi 07aedd195f Unify AI Usage of Shield Manage Delay (#6589)
* Unify AI Usage of Shield Manage Delay

`$AI Shield Manage Delay:` can be set in the AI profiles, but also set for AI specific classes. Currently, The values set for the specific AI class are only used `if (iff_x_attacks_y(Player_ship->team, Ships[objp->instance].team))`, otherwise the values in the AI profile are used.

Being able to have the shield delay work in a unified way according to the AI class values is very useful, especially for friendly wingmen that you want have different shield manage delays.

This PR adds a AI profiles flag to unify Shield Manage Delay behavior, so that it always works in the same way regardless of AI team conditions.

* cleanup unifying behavior along with CM firing chance

* restore what sushi had for now
2025-02-12 14:29:23 +00:00
Goober5000 e3d11dfb31 Merge pull request #6570 from Goober5000/order_updates
update script API for AI orders
2025-02-10 22:01:21 -05:00
Goober5000 133b033ea0 some upgrades to vmallocator.h
Make `SCP_vector`, `SCP_list`, `SCP_set`, and `SCP_multiset` subclasses of their STL counterparts.  This allows helper functions to be added to each, including the `contains()` function which is not available until C++20.  (This has minor effects on other parts of the codebase that treated `SCP_` and `std::` as synonyms.)  Since there is now a nontrivial amount of code that needs to be in a .cpp file, create vmallocator.cpp.

Note that `SCP_string` truncation has been refactored to use length rather than C-string size to be consistent with other `std::basic_string` functions.

Also, remove some enum class hashing code which is no longer necessary now that the codebase is at C++17.
2025-02-02 14:54:26 -05:00
Goober5000 89e2f264fd update script API for AI orders
Allow waypoint orders to target a specific waypoint and go in reverse.  Add script access to waypoints and waypoint lists belonging to orders.

Fix assignment of targets and goal start times.  Assign targets when they are different (and thus need to be assigned), not when they are the same.
2025-02-02 12:04:43 -05:00
Goober5000 ca624f86e2 a few pstypes changes and refinements
Several changes related to the volumetrics model fix:
1. Update the `model_load()` prototype with better parameter names, including a new `ErrorType` enum defined in pstypes.h
2. Update the `MIN`, `MAX`, and `CLAMP` macros, all of which were susceptible to performance issues because they duplicated their tokens.  This could cause functions or calculations to be re-run unnecessarily.  Since the codebase is now on C++17, `std::clamp()` can be used.
3. Update the rest of the codebase to fix any associated issues.
2025-01-30 21:35:03 -05:00
wookieejedi c4341518bb Allow Modders to Tune Guarding (#6544) 2025-01-23 19:14:51 -05:00
Goober5000 04068fc83a fix kamikaze attack of subsystems
Kamikaze ships entering broadside mode (`ai_cruiser_chase()`) would always point toward the center of the ship.  With this fix, they will point toward a subsystem if attacking one.  (In practice this situation had not been encountered before, since by default the ai-destroy-subsystem order is only available to fighters and bombers, which do not use broadside mode.)
2025-01-16 15:47:49 -05:00
Goober5000 8baee0470a refactor AIG_TYPE_* into enum classes 2025-01-02 14:05:21 -05:00
Goober5000 c38882a51e refactor AI_GOAL_* #defines into enums, and remove bitwise operations on them [non-FRED] 2025-01-02 14:05:21 -05:00
Goober5000 84f1b2be9f Merge pull request #6411 from Goober5000/all_the_consts
all the consts!
2024-11-23 14:36:56 -05:00
Goober5000 1bcad06461 remove the avoid-shockwave flag when too far away from a weapon; and add a should-be-dead check 2024-11-14 00:07:26 -05:00
Goober5000 722ed575d2 only refine the expected homing pos if it really does need to be refined 2024-11-14 00:07:18 -05:00
Goober5000 5dfe398af2 add a cache for the expected homing pos 2024-11-14 00:07:06 -05:00
Goober5000 03bf756d5c fix ai behavior when avoiding ship and weapon shockwaves
The FreeSpace AI includes behavior to run away from shockwaves to try to avoid explosion damage.  This is implemented for both ships and missiles, although the missile implementation has been buggy ever since retail.  There are several issues:

1. The AI attempts to evade a weapon immediately upon launch.  Many missiles have a period of free flight before they start homing on their target.  During the free flight period, the homing position is not set.  Consequently, the AI will not know the position to avoid and will make assumptions.
2. The AI assumes the shockwave-producing weapon will detonate at the center of the ship it is targeting, although missiles detonate on the surface.  A capital ship is likely to be significantly larger than the weapon's blast radius, leading the AI to believe the detonation will be much farther away than it actually is.
3. When avoiding ship shockwaves, the `shockwave_object` field is not cleared when avoiding is complete.  This can result in stale references (though not crashes, due to object type checks), causing AI to not avoid future explosions in certain cases.
4. When avoiding ship shockwaves, the code does not check the correct ship for the amount of damage caused by the shockwave.

All of these issues are now fixed.  Since this is a change in AI behavior, these fixes are tied to a new AI profiles flag.
2024-11-12 00:40:05 -05:00
Goober5000 524ecbe826 handle clang warnings 2024-11-07 23:33:13 -05:00
Goober5000 31a86e3f3d all the consts!
Some experimentation resulted in a lot of const-cascading.  This PR applies `const` to a large number of functions.
2024-11-07 23:33:13 -05:00
Goober5000 385bf49b39 replace waypoint pointers with waypoint indexes
Several locations in the code (notably `ai_info` and `waypoint_h`) previously stored pointers for waypoints and waypoint lists that need to be valid for more than one frame.  Since both waypoint lists and waypoint paths may change size, resulting in vector reallocation, the information should be stored as indexes instead.

Fixes #6353.
2024-09-21 01:00:24 -04:00
Goober5000 704bf95a75 implement don't-change-position and don't-change-orientation
Per #4294, this adds two new flag that, together, are equivalent to the legacy "immobile" flag but individually provide more fine-grained control.  Since the physics for position and orientation have a bit of overlap, the new flag functionality simply reverts whatever position or orientation changes were done by the physics sim.

In the FRED and qtFRED ship flags dialogs, the "immobile" flag (aka "Does Not Move") is replaced by the two new flags, but in FSO the situation is more complicated.  The old and new flags coexist and have overlapping functionality, so the mission designer must be careful to set and unset the appropriate flag (that is, it is possible for both "immobile" and "don't-change-position" to be set at the same time, in which case the ship cannot change position without both flags being unset).  The `set-immobile` and `set-mobile` legacy SEXPs ensure that the new flags are removed so as not to conflict with the legacy flag, but the `alter-ship-flag` and `are-ship-flags-set` SEXPs and the `getFlag` and `setFlag` script functions use exactly what the FREDder/scripter specifies and nothing else.

(I attempted to completely replace "immobile" with the two newer flags, but it proved intractable to do this while maintaining reverse compatibility: retrofitting single-flag functions to apply dual flags for one particular situation made the functions harder to understand as well as introducing many edge cases and opportunities for bugs or "gotchas".)

Implements #4294.
2024-08-17 16:46:48 -04:00
Asteroth 8914ac6e2d guards ignore protected attackers (#6253) 2024-07-16 15:46:12 -04:00
wookieejedi f5a268bcef Expose Minimum LoS Detection Radius (#6252)
The somewhat modern AI flag for `Require_exact_los` is extremely useful in multiple different situations, for example making large capital ship weapons not fire into friendly surrounding ships on accident. When the feature was added, the `check_los` function was provided with a default value of `10.0` meters for minimum radius detection. This PR exposes that value to modders so they can set it to a reasonable value depending on the ship sizes of their specific needs. And as a note, the los value just uses a simple numerical comparison check, so the los detection could be set to a negative value with no consequence (hence why error messages were not added, though if folks would like I can certainly add them).
2024-07-15 08:30:31 -04:00
Mike Nelson 7f20b9cf64 Unlimited Asteroid/Debris Field Types & Subtypes (#6021)
* base working version of unlimited debris types

* Proper FRED support

* make qtFRED handle asteroids properly

* fix qtFRED assertion

* further clean out MAX_RETAIL_DEBRIS_TYPES

* These should be sizes not POFs

* Move this to it's own method

* convert asteroid subtypes to a vector

* unlimit asteroid subtypes

* fix some global shadowing

* signed/unsigned mismatch

* Address feedback
2024-06-10 22:06:10 +02:00
Goober5000 d5e387479e replace pointers with indexes in object_h
This refactors the object_h struct to store objects as indexes rather than pointers.  This is a prerequisite for making the collection of objects dynamic in the future.  See also #6100.

Follow-up to #5800 and #5829.
2024-06-09 02:06:22 -04:00