368 Commits
Author SHA1 Message Date
Goober5000andClaude Fable 5.1 11781a5081 Offscreen indicator: read the clipped vertex before freeing it and fix the min-side clamps
The overflow flag and screen position were read from a temp vertex
already returned to the pool, which only worked because nothing
allocated in between. Copy them out first.

The clamps that keep the indicator away from the top and left of the
clip rectangle subtracted the clip origin instead of adding it, and
dropped it from the clamped value. Inert while the origin is zero, but
wrong for any other clip rectangle.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-09-23 00:07:19 -04:00
Goober5000andClaude Fable 5.1 12323ef3f6 Fix the offscreen target indicator drawing with uninitialized position and direction
HudGaugeOffscreen::calculatePosition could return without writing its
outputs, either when the eye vertex overflowed or when the clipped
point landed a pixel or two inside the clip rectangle. The caller drew
anyway, so the indicator appeared at a random spot with a random
orientation that changed every frame. This showed up when targeting a
subsystem on a distant ship, where the clip loses enough precision to
hit the second case.

calculatePosition now returns whether it found a position, and both
callers skip drawing when it did not. The inside case falls back to
the nearest screen edge, since the point is already known to be off
screen, so the indicator stays steady instead of blinking.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-09-18 00:50:39 -04:00
Goober5000andClaude Fable 5.1 0f15b1b637 Store the targeted subsystem position in the current target's display list entries
The projected point was the subsystem but the stored position was the
hull centre, so the offscreen indicator sized its triangles for a
different point than the one it clipped against.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-09-18 00:50:39 -04:00
Goober5000andClaude Fable 5 d413e52b33 External weapon model refactor: consolidate per-bank state, fix design issues (#7634)
* consolidate per-bank external weapon model state into one struct

The state for a bank's external weapon model was scattered across two
structs and three naming schemes: the model instance and its weapon in
ship_weapon, the firing-point chain counter in a combined array indexed
with the bank + MAX_SHIP_PRIMARY_BANKS packing convention, and the
gatling spin rate/angle on ship itself.

Gather them into external_weapon_state, with one array for primary
banks and one for secondary banks in ship_weapon.  This removes the
index-packing convention (ship_get_external_model_fp_offset now takes
the bank's state directly), and ship_delete now also frees secondary
instances should a future weapon ever create one.

No behavior change.

Co-Authored-By: Claude Fable 5 <[email protected]>

* update external weapon model spin in per-frame processing

The gatling spin state was integrated inside ship_fire_primary (spin-up)
and ship_stop_fire_primary_bank (spin-down), which entangled a per-frame
visual/state update with the firing event paths and had two quirks:

- Spin-down only ran when something called the stop-fire path, so a bank
  could freeze mid-spin (e.g. AI ships that stop calling
  ship_fire_primary without going through ship_stop_fire_primary).
- The spin angle only advanced when the ship class set
  $Show Primary Models:, so identical weapon state evolved differently
  per ship class even though the rate (which gates firing) always ran.

Banks that try to fire now just request spin-up, and
update_external_weapon_spin() -- called from ship_process_post next to
update_reload_percent(), which was moved out of the physics code for
the same reason -- integrates the rate and angle every frame.  The
firing gate (no shots until the barrels reach full speed) is unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>

* make external_model_num mean what it says; gate firing offsets on drawn models

Since the original 2005 implementation, external_model_num was bashed
to model_num at parse time when no $External Model File: was given, so
every POF-rendered weapon appeared to have an external model.  All the
checks downstream therefore tested 'has any model' while reading as
'has an external model', and two paths leaked the fallback into
gameplay:

- ship_fire_secondary applied the external-model firing point offset
  without checking $Show Secondary Models: (unlike the primary path),
  so any missile whose own in-flight POF contained gun points would
  have its launch position offset on every ship in the game.
- The AI line-of-sight check, copied from ship_fire_secondary,
  applied the offset with no draw check for either bank type, so for
  primaries it could disagree with the actual firing path.

external_model_num now stays -1 unless $External Model File: was
given.  Rendering code (ship_render_weapon_models, the HUD hardpoints
gauge, and the gatling instance helper) falls back to the weapon's own
model at the point of use, preserving the existing display of missiles
on hardpoints.  Firing point offsets now require a dedicated external
model *and* the bank's models to actually be drawn, in all three
paths.  In practice this does not change firing behavior, because GPNT
chunks are never added to weapon models except when explicitly built
as external models.

Co-Authored-By: Claude Fable 5 <[email protected]>

* warn on external weapon model conventions that are silently ignored

Two load-bearing conventions of the external weapon model feature had
no diagnostics:

- Only the first gun bank of an external model supplies firing points;
  additional banks are silently ignored.  Warn when an external model
  has more than one gun bank.
- $Show Primary Models: / $Show Secondary Models: must list exactly
  one bool per weapon bank; missing entries silently default to not
  drawing.  Warn when the list length doesn't match the bank count.

Co-Authored-By: Claude Fable 5 <[email protected]>

* orient external weapon models along their slot's firing normal

External weapon models were always rendered pointing ship-forward, and
reloading missiles slid backward along fixed -Z, regardless of the gun
or missile point's normal -- so a bank angled 45 degrees outward fired
along its normal while its mounted model pointed dead ahead.  The
per-slot angle offset was also applied as a roll about ship-forward
rather than about the firing axis.

ship_get_weapon_model_slot_transform now builds the slot orientation
from the firing normal (identity for the usual dead-ahead normal, so
existing forward-facing mounts render exactly as before) with the
angle-offset roll applied about that axis, and slides reloading
missiles along the normal.

Correspondingly, ship_get_external_model_fp_offset now rotates the
external model's firing-point offset by the same slot transform, so
shots continue to emerge from the drawn barrels on angled mounts, and
now also respect the angle-offset roll (previously the offset was
added unrotated, mismatching rolled models).

Co-Authored-By: Claude Fable 5 <[email protected]>

* address review: same-frame spool-up gating; optional slot transform outputs

Move the update_external_weapon_spin() call from ship_process_post() to the top of ship_process_pre().  The update now happens before any firing attempt in the frame, so the spool-up firing gate sees the current frame's spin rate, as it did on master where the update happened inside the firing code.  This also makes the ordering consistent for the AI regardless of Framerate_independent_turning.

Also make both outputs of ship_get_weapon_model_slot_transform() optional.

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: Claude Fable 5 <[email protected]>
2026-08-27 22:08:06 -07:00
Goober5000 4e26511055 Merge pull request #7635 from Goober5000/fix/firingpoints
fix dual fire handling for banks that cannot use it
2026-07-28 19:42:24 -04:00
Goober5000andClaude Fable 5 60c554354c fix dual fire handling for banks that cannot use it
The Secondary_dual_fire flag is per-ship, but not every secondary bank
can dual fire.  Previously the HUD drew the dual fire link icon even
when the armed bank had only one firepoint, and firing such a bank
cleared the flag entirely, silently discarding the player's dual fire
preference for the other banks.

Extend the ignored-rather-than-cleared approach already established
for no-doublefire weapons to the single-firepoint case:

- add ship_secondary_bank_can_dual_fire() as the single source of truth
  for bank validity, No_doublefire, ai_profiles restrictions, and
  firepoint count
- ship_fire_secondary() now fires single and leaves the flag alone when
  the bank cannot dual fire, instead of removing the flag
- the weapons gauges and the hardpoints gauge consult the helper, so the
  link icon and second-firepoint highlight are no longer drawn for
  incapable banks
- the dual fire toggle on a single-firepoint bank now explains why it
  cannot dual fire and leaves the preference untouched
- restoring the saved dual fire preference no longer depends on bank 0's
  firepoint count, since incapable banks now simply ignore the flag
- key control and force feedback also consult the helper

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-28 01:08:07 -04:00
Goober5000andClaude Fable 5 009529b235 guard against empty weapon banks in external model render paths
ship_render_weapon_models(), HudGaugeHardpoints::render(), and
ship_stop_fire_primary_bank() indexed Weapon_info[] with the bank's
weapon index without checking for an empty bank (-1).  Banks within
num_*_banks are normally populated, but other code that iterates banks
treats them as possibly empty and guards accordingly (e.g.
ship_do_cargo_hidden, ship_page_in), so these paths now do too.  In
the hardpoints gauge an empty primary bank still draws its firing
point circles.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-13 13:19:23 -04:00
Goober5000andClaude Fable 5 90ea116d0f recreate external weapon model instance when a bank's weapon changes
The model instance used to spin Gun_rotation submodels (gatling
barrels) was created lazily on first render, guarded by a
once-per-bank bool that nothing ever reset.  If a primary bank's
weapon changed mid-mission (set-primary-weapon SEXP, scripting, rearm
precedence swap, multiplayer sync), the old weapon's instance leaked
until the ship was deleted and was silently discarded at render time
by the model/instance mismatch guard, while the new weapon never got
an instance, so its barrels never spun.

Replace the bool with a record of which weapon the instance was
created for, and move the logic into
ship_get_external_weapon_model_instance(), which recreates the
instance whenever the bank's weapon differs.  This self-heals after
every swap path without needing hooks in each of them.  The HUD
hardpoints gauge now goes through the same function instead of reading
the possibly-stale instance array directly.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-13 13:19:23 -04:00
Goober5000andClaude Fable 5 368a6a9053 consolidate external weapon model slot placement; fix HUD launcher position
The per-slot placement math for rendered external weapon models (the
angle-offset yaw plus the reload slide-back for missiles) was
duplicated four times across ship_render_weapon_models() and
HudGaugeHardpoints::render().  Extract it into
ship_get_weapon_model_slot_transform() so that future changes (such as
submodel-attached weapon banks) only need to touch one place.

This fixes a bug in the hardpoints gauge: the launcher-style secondary
branch computed the view-rotated world position but then rendered the
model at the untransformed local point, misplacing launcher models in
the rotated top/front views.

Also: hoist per-bank invariants out of the slot loops, drop the
redundant render_flags parameter of ship_render_weapon_models() (it
always equaled the flags already set on the render params), remove
long-dead commented-out code, and read the reload percentage once per
slot.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-13 13:19:23 -04:00
Goober5000 e71e9494d8 "change atan2_safe to atan2" followup (#7531)
Follow-up to #6255.  Partially restore use of `atan2_safe` from 87ea04fa53: keep special case while delegating most cases to atan2.

Fixes #7530.
2026-06-18 10:34:16 +09: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
Goober5000 2436ef2a3f Merge pull request #7129 from Goober5000/fix_scannable_ship_types
enhance and clean up scannable ship types
2026-02-23 22:00:34 -05:00
Goober5000 306f34a574 update force_fit_string and bounds in calling functions (#7161)
Rewrite `force_fit_string` to be more robust and to remove the potential for memory access violations.  Also audit all of the length parameters in the calling functions - some of them were the buffer size, not the length.  This should fix the mysterious crash in the mission log screen.
2025-12-25 08:08:46 +09:00
Goober5000 32c07ef847 enhance and clean up scannable ship types
The `$Scannable` field in objecttypes.tbl controls whether a ship can be targeted by the "target unscanned ship" key.  This is independent of which ship types can actually be *scanned*, which is not connected to any field in objecttypes.tbl.

So, this PR renames the `$Scannable` field to `$Targetable as unscanned:` to be clearer about what it actually does, and it adds the missing counterpart `$Scannable by default:` flag to specify which ship types can actually be scanned.

Note that this is only relevant for legacy scanning behavior.  Mods which use `$Unify scanning behavior:` do not need either of the ship type flags.
2025-11-29 13:48:46 -05:00
wookieejedi 38acf9ae15 Allow mods to easily tune 'Target Hostile Bomb or Bomber' control (#7048)
* Allow mods to easily tune 'Target Hostile Bomb or Bomber' control

The `Target Hostile Bomb or Bomber` control targets bombs or bombers, though in mods may want a way to easily set exactly what this control does while still leveraging the utility of the 'bomb' flag for weapons and/or 'bomber' flag for ships (both of which affect AI behavior in various ways).

For example, in Solaris, Darius wants a way a way to allow players to only target bombs, while still allowing ships to be classified as as 'bomber'. In this case, the 'only bombs' option can be used. This PR adds a `$Behavior for pressing 'Target Hostile Bomb or Bomber' control:` to allow mods to set the behavior to `default` `only bombs` or `only bombers`.

For example, in FotG we want to allow players to target just bombers, as bombs themselves are hard to shoot down (critically though we still want the player to be able to manually target a bomb via 'target object in reticle'. This allows players to prioritize and easily target bombers while allowing skilled pilots to retain the option of manually shooting down bombs if they want an extra challenge. In this case the 'only bomber' option can be used.

Tested and works as expected in all cases.

* update using cleaned up function

* extra alt option for default case

* warning cleanup

* update default message

* whitespace clang

* appease clang with brackets
2025-10-15 16:47:45 -04:00
wookieejedi 6b5f9c9be7 Cleanup and fix hud_target_missile (#7051)
* Cleanup and fix `hud_target_missile`

The function `hud_target_missile` is what runs when the `Target Hostile Bomb or Bomber` control is pressed. Notably, it does not work if there is only 1 hostile bomber. The issue is that the loop within `hud_target_missile` is designed to search for the "next" target, but when there's only one valid target, the loop logic skips over it. But, `hud_target_missile` correctly will target if there is only 1 bomb though, so the fix is to just update the ship bomber logic similarly. This PR updates the logic of the bomber search so it matches the bombs search, that way the bomber search can target a bomber if there is only 1 present in the mission (just as how the existing bomb search will let you target 1 bomb if there is only 1 bomb in the mission currently).

This PR also takes the opportunity to cleanup the function with more clear variable and function naming, along with two new internal bools which will be very helpful for later planned PRs.

Fixes #7050. Tested and works as expected.

* rework hud_target_hostile_bomb_or_bomber

* appease clang

* additional cleanup and modernization

* more cleanup for select previous
2025-10-13 21:51:50 -04:00
Mike Nelson 829edaaa3e Fix Lua API polluting the game context (#6739)
* split rendering values into lua and fso contexts

* address feedback
2025-06-04 09:43:33 -04:00
Mike Nelson 25dbfc666b Misc hud config fixes (#6727)
* Fix crash when clicking inside the hud area but not on a gauge

* add dogfight_variant weapon flag for sorting purposes

* fix weapon display for default hud and skip dogfight weapons on auto-select

* cleanup the mouse check function
2025-05-27 08:55:12 -04:00
Goober5000 067c65773a coverity: possible use of invalid iterator (especially with jump nodes) 2025-05-10 01:15:46 -04:00
Mike Nelson 524d36332d per-hud config weapons and shield gauge ship (#6651) 2025-04-17 08:31:48 -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
Mike Nelson 60777154be Hud config upgrades (#6581)
* base working version

* localization and minor tweaks

* add retail UI controls

* Add way to hide gauges from Config view

* convert hud config from index matching to id matching

* clang
2025-03-22 22:47:38 -04:00
Mike Nelson a7754e637a Fix hud errors (#6627)
* fix weapon energy text in config mode

* fix x,y  mismatch

* fix squad message copy/paste error
2025-03-09 21:09:37 -04:00
Goober5000 9e4287e7b0 Merge pull request #6596 from Goober5000/vm_vector_2_matrix_norm
use vm_vector_2_matrix_norm on normalized vectors
2025-03-03 21:00:02 -05:00
Mike Nelson 681206a356 config rendering methods for 3d targeting gauges (#6577) 2025-03-03 08:24:05 -05: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
Mike Nelson 5b736b1ddc Hud config framework (#6495)
* Add scaling for vfnts and scale multipler to gr_string

* nullptr

* Add scaling to a couple other string operations

* Add scaling features to additional bitmap drawing methods

* Implement framework for rendering hud gauges in config mode

* fix merge error

* address feedback
2025-02-03 07:01:47 -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
Asteroth 590cfa5f81 Merge pull request #6293 from Baezon/remove-some-int3s-from-hud-code
Remove some int3s from offscreen indicator drawing
2024-08-14 18:10:29 -04:00
Asteroth c64a3e5e6a shouldnt lose target either 2024-08-13 13:26:04 -04:00
Asteroth 65d281e3cb remove some int3s 2024-08-13 01:40:31 -04:00
JohnAFernandez a9fdbac199 Fix one comment's accuracy 2024-08-10 01:29:05 -04:00
JohnAFernandez 01d3e14dc1 Fix renderLeadCurrentTarget
This fixes an edge case in the Average and Multiple modes, where secondary lead indicator rendering was skipped by continue statements when the primary weapons were out of range.   I also added some comments that help people actually understand what this function does as it is very confusing.
2024-08-10 01:09:10 -04:00
Goober5000 87ea04fa53 change atan2_safe to atan2
According to DahBlount:
> Most atan2 implementations for a long time had problems when the inputs were close to machine epsilon or when x was less than or equal to 0

And according to z64555:
> Modern versions of C++ should have an atan2() that works correctly when the inputs are at 0 or near epsilon of the floating point specification, meaning Volition's workaround shouldn't be needed anymore.

So this removes `atan2_safe` and changes all instances to `atan2`.  Consequently, the angle range in FRED now shows as [-180, 180] as expected.

Fixes #3730.
2024-07-15 20:55:57 -04:00
John Fernandez 1f834aa406 change render flags to uint64_t
32 more flags, yay!
2024-07-04 18:08:27 -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
Mike Nelson 4432341694 Scanning is chaos, let's make it sane (#6000)
* Unify scanning behavior

* Allow modifying the cargo title

* update localization

* update documentation

* update localization

* address feedback

* fix conflict merge resolution error

* fix oversights, missed subsys title # feature, additional xstrs

* don't need sprintf here
2024-06-08 22:20:22 +02:00
Goober5000 d51f53fb30 add ship flag to disable message scrambling when EMP is active
What it says on the tin.  This supersedes PR #2917, as it puts the flag check in the proper function and uses a ship flag rather than a mission flag.
2024-05-11 23:38:18 -04:00
joverral d16f46fb1c name change per feedback 2024-02-21 16:42:30 -08:00
joverral 77ad3ba0aa Fixes per feedback 2024-02-21 10:25:52 -08:00
joverral 25d326567e allow targeting subsystems on already targeted ship under reticule 2024-02-20 18:07:13 -08:00
Goober5000 f4669db41c constify some turret code
Apply `const` to a whole bunch of functions.  Also change a bunch of ints to bools.
2023-12-30 00:00:31 -05:00
Goober5000 24a4c0df6a protect eval_enemy_obj_struct input values
Add `const` to the `eval_enemy_obj_struct` struct so its values aren't inadvertently overwritten.  Handle a bit of const-correctness propagation.  Also add default member initialization to the struct.
2023-11-06 00:05:54 -05:00
Goober5000 4fbe3b7131 make sure class_type exists when used
A mission in Mantle caused an invalid vector access when checking whether to vaporize a ship whose ship type was -1.  Turns out having "no type" is actually allowed, so I audited all `class_type` accesses throughout the code for potential -1 accesses.  Most were properly checked but a few were not.  A few others needed to be tweaked.

(I suspect this is something that would be on the Coverity list if it isn't already.)
2023-10-29 15:17:22 -04:00
Asteroth aed90493ec make g3_project_vertex unconditionally project 2023-10-23 17:53:30 -04:00
BMagnu 02e84f212a Fix HUD slewing (#5656)
* Fixup HUD for slewing

* Fix Training Message box

* Lead indicator slew

* Fix HUD Message Head

* Correctly render messages

* Fix HUD offset handling

* HUD offset never makes any sense

* Cleanup offset code

* Remove unused parameter

* Fix Talking Head in RTT mode
2023-10-16 01:44:17 +09:00
Asteroth bc09f0672b better hud gauge load safety 2023-07-07 01:20:11 -04:00
Goober5000 4ff90d3716 move some utilities to a better location, plus a slight refactor 2023-05-28 22:47:38 -04:00
Goober5000 5b175673ed add initializers to mc_info
This initializes `mc_info` to the correct values when it is constructed, removing the need for `mc_info_init`.

This PR also fixes a group of incorrectly inverted assignments in beam.cpp.
2023-02-21 19:09:10 -05:00
Goober5000 782980844b add checks to Missile_obj_list and Asteroid_obj_list 2023-01-01 18:13:29 -05:00