Files
Boutros Tawaifi 49464c56f6 Add an "On Ship Damage" script hook (#7894)
* Add an "On Ship Damage" script hook

Runs after resolved damage has been applied to a ship (lethal hits run before
the destruction callback). Hook variables: Target, Source, Attacker,
WeaponClass, WeaponSubtype, SourceKind (PROJECTILE, BEAM, SHOCKWAVE, COLLISION,
OTHER), DamageType, ShieldDamage, HullDamage, Quadrant, Ballistic, BeamWeapon
and LethalHit. Conditions can filter on the target ship. Nothing is computed
unless a script has registered the hook.

* Move the On Ship Damage hook call out of a lambda

ship_do_damage() now calls a file-level static function, ship_run_damage_hook(), that takes the values it needs as arguments instead of capturing the function state. Hook behaviour is unchanged.

* Report subsystem damage in the On Ship Damage hook

Adds SubsystemDamage, the total subsystem strength a damage application removed, so a hit absorbed by subsystems no longer looks like a small hull hit. The hook description now says it only runs where damage is applied, which excludes multiplayer clients.

* Address On Ship Damage review comments

- Remove WeaponClass, WeaponSubtype, Ballistic and BeamWeapon from the
  hook. Scripts read these from Source when it is a weapon.
- Add a "Weapon class" condition for the On Ship Damage hook. It matches
  only when the damage source is a weapon of that class; a weapon name
  that does not exist never matches.
- SubsystemDamage is now a table keyed by subsystem handle that lists only
  the subsystems that lost strength. The total is removed.
- Rename PROJECTILE to NON_BEAM_WEAPON.
- Classify SourceKind by damage source: COLLISION for ship, debris and
  asteroid collisions; EXPLOSION for ship-death and docked-ship blasts and
  asteroid blasts; WASH for engine wash; SELF for kills where the ship
  damages itself; OTHER for other ship-sourced damage.
- Reword the hook description.

* Key On Ship Damage SubsystemDamage by canonical name

Lua tables cannot use subsystem handles as keys in a usable way, so key
the SubsystemDamage table by each subsystem's CanonicalName instead.

* Tag ship-vs-prop damage as a collision and use in_bounds

The ship-vs-prop collision call didn't pass DamageSource::Collision, so
the On Ship Damage hook reported those hits as OTHER. Every other
collision path was already tagged.

Also use SCP_vector::in_bounds() for the Weapon_info and Damage_types
index checks in ship_run_damage_hook().
2026-11-06 18:41:29 -05:00

107 lines
5.2 KiB
C++

/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include "weapon/weapon.h"
#ifndef _SHIPHIT_H
#define _SHIPHIT_H
struct vec3d;
class ship;
class ship_subsys;
class object;
#define NO_SPARKS 0
#define CREATE_SPARKS 1
#define MISS_SHIELDS -1
constexpr float DEATHROLL_ROTVEL_CAP = 6.3f; // maximum added deathroll rotvel in rad/sec (6.3 rad/sec is about 1 rev/sec)
// ===================== NOTE!! =========================
// To apply damage to a ship, call either ship_apply_local_damage or ship_apply_global_damage.
// These replace the old calls to ship_hit and ship_do_damage...
// These functions do nothing to the ship's physics; that is the responsibility
// of whoever is calling these functions. These functions are strictly
// for damaging ship's hulls, shields, and subsystems. Nothing more.
// handle a -destroyed submodel in the special case where it is related to a special-point subsystem
// (the usual submodel-submodel case is handled through the normal code paths)
void check_subsystem_submodel_link(const ship *shipp, const ship_subsys *subsys, bool was_destroyed);
// when a destroyed subsystem has been repaired, bring back its submodels in place of any -destroyed form
void restore_subsystem_submodels(const ship *shipp, const ship_subsys *subsys);
// hide a destroyed subsystem's submodels in favor of any -destroyed form, unless the subsystem is flagged not to disappear
void blow_off_subsystem_submodels(const ship *shipp, const ship_subsys *subsys);
// whether a subsystem's submodels are currently blown off, or its linked -destroyed submodel is shown
bool subsystem_submodels_blown_off(const ship *shipp, const ship_subsys *subsys);
// function to destroy a subsystem. Called internally and from multiplayer messaging code
extern void do_subobj_destroyed_stuff( ship *ship_p, ship_subsys *subsys, const vec3d *hitpos, bool no_explosion = false );
std::pair<std::optional<ConditionData>, float> do_subobj_hit_stuff(object *ship_obj, const object *other_obj, const vec3d *hitpos, int submodel_num, float damage, bool *hull_should_apply_armor, float hit_dot = 1.f, bool shield_hit = false);
// Goober5000
// (it might be possible to make `target` const, but that would set off another const-cascade)
extern void ship_apply_tag(ship *ship_p, int tag_level, float tag_time, object *target, const vec3d *start, int ssm_index, int ssm_team);
// Where a ship damage application came from, for the On Ship Damage hook. Inferred uses the damaging object type and
// the engine wash flag. Collision and Explosion are for callers whose damaging object cannot tell them apart.
enum class DamageSource {
Inferred,
Collision,
Explosion,
};
// This gets called to apply damage when something hits a particular point on a ship.
// This assumes that whoever called this knows if the shield got hit or not.
// hitpos is in world coordinates.
// if quadrant is not -1, then that part of the shield takes damage properly.
// (it might be possible to make `other_obj` const, but that would set off another const-cascade)
void ship_apply_local_damage(object *ship_obj, object *other_obj, const vec3d *hitpos, float damage, int damage_type_idx, int quadrant, bool create_spark=true, int submodel_num=-1, const vec3d *hit_normal=nullptr, float hit_dot = 1.f, const vec3d* local_hitpos = nullptr, DamageSource source = DamageSource::Inferred);
// This gets called to apply damage when a damaging force hits a ship, but at no
// point in particular. Like from a shockwave. This routine will see if the
// shield got hit and if so, apply damage to it.
// You can pass force_center==NULL if you the damage doesn't come from anywhere,
// like for debug keys to damage an object or something. It will
// assume damage is non-directional and will apply it correctly.
void ship_apply_global_damage(object *ship_obj, object *other_obj, const vec3d *force_center, float damage, int damage_type_idx, DamageSource source = DamageSource::Inferred);
// like above, but does not apply damage to shields
void ship_apply_wash_damage(object *ship_obj, object *other_obj, float damage);
// next routine needed for multiplayer
void ship_hit_kill(object *ship_obj, object *other_obj, const vec3d *hitpos, float percent_killed, bool self_destruct = false, bool always_log_other_obj = false);
void ship_self_destruct( object *objp );
// Call this instead of physics_apply_whack directly to
// deal with two docked ships properly.
void ship_apply_whack(const vec3d *force, const vec3d *hit_pos, object *objp);
// externed for code in missionparse to create sparks on a ship with < 100% hull integrity.
void ship_hit_sparks_no_rotate(object *ship_obj, vec3d *hitpos);
// externed so that ships which self destruct have the proper things done to them in multiplayer
void ship_generic_kill_stuff( object *objp, float percent_killed );
// find the max number of sparks allowed for ship
// limited for fighter by hull % others by radius.
int get_max_sparks(const object* ship_obj);
// player pain
void ship_hit_pain(float damage, int quadrant);
#endif //_SHIPHIT_H