Skip to content

Commit

Permalink
all the consts!
Browse files Browse the repository at this point in the history
Some experimentation resulted in a lot of const-cascading.  This PR applies `const` to a large number of functions.
  • Loading branch information
Goober5000 committed Nov 3, 2024
1 parent f71f02e commit ac050dd
Show file tree
Hide file tree
Showing 58 changed files with 251 additions and 242 deletions.
2 changes: 1 addition & 1 deletion code/ai/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ extern void ai_dock_with_object(object *docker, int docker_index, object *dockee
extern void ai_stay_still(object *still_objp, vec3d *view_pos);
extern void ai_do_default_behavior(object *obj);
extern void ai_start_waypoints(object *objp, waypoint_list *wp_list, int wp_flags, int start_index);
extern void ai_ship_hit(object *objp_ship, object *hit_objp, vec3d *hit_normal);
extern void ai_ship_hit(object *objp_ship, object *hit_objp, const vec3d *hit_normal);
extern void ai_ship_destroy(int shipnum);
extern vec3d ai_get_acc_limit(vec3d* vel_limit, const object* objp);
extern void ai_turn_towards_vector(const vec3d *dest, object *objp, const vec3d *slide_vec, const vec3d *rel_pos, float bank_override, int flags, const vec3d *rvec = nullptr, const vec3d* turnrate_mod = nullptr);
Expand Down
4 changes: 2 additions & 2 deletions code/ai/aibig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ int ai_big_maybe_follow_subsys_path(int do_dot_check=1);
void ai_big_strafe_position();
static int ai_big_strafe_maybe_retreat(const vec3d *target_pos);

extern void compute_desired_rvec(vec3d *rvec, vec3d *goal_pos, vec3d *cur_pos);
extern void big_ship_collide_recover_start(object *objp, object *big_objp, vec3d *collision_normal);
extern void compute_desired_rvec(vec3d *rvec, const vec3d *goal_pos, const vec3d *cur_pos);
extern void big_ship_collide_recover_start(const object *objp, const object *big_objp, const vec3d *collision_normal);


// Called by ai_big_pick_attack_point.
Expand Down
12 changes: 6 additions & 6 deletions code/ai/aicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ int ai_is_stealth_visible(const object *viewer_objp, const object *stealth_objp)
* Forward vector is the forward vector of the ship.
* If from_dot == NULL, don't fill it in.
*/
float compute_dots(object *objp, object *other_objp, float *to_dot, float *from_dot)
float compute_dots(const object *objp, const object *other_objp, float *to_dot, float *from_dot)
{
vec3d v2o;
float dist;
Expand Down Expand Up @@ -7240,7 +7240,7 @@ static void ai_chase_eb(ai_info *aip, vec3d *predicted_enemy_pos)
// Assumes ship_objp is not moving.
// Returns negative time if not going to hit.
// This is a very approximate function, but is pretty fast.
float ai_endangered_time(object *ship_objp, object *weapon_objp)
float ai_endangered_time(const object *ship_objp, const object *weapon_objp)
{
float to_dot, from_dot, dist;

Expand Down Expand Up @@ -7693,7 +7693,7 @@ int maybe_avoid_big_ship(object *objp, object *ignore_objp, ai_info *aip, vec3d
* Set desired right vector for ships flying towards another ship.
* Since this is governed only by vector to target, it causes ships to align bank and look less chaotic.
*/
void compute_desired_rvec(vec3d *rvec, vec3d *goal_pos, vec3d *cur_pos)
void compute_desired_rvec(vec3d *rvec, const vec3d *goal_pos, const vec3d *cur_pos)
{
vec3d v2e;

Expand Down Expand Up @@ -15918,7 +15918,7 @@ int firing_aspect_seeking_bomb(object *objp)
// *objp collided with big ship *big_objp at some global point.
// Make it fly away from the collision point.
// collision_normal is NULL, when a collision is imminent and we just want to bug out.
void big_ship_collide_recover_start(object *objp, object *big_objp, vec3d *collision_normal)
void big_ship_collide_recover_start(const object *objp, const object *big_objp, const vec3d *collision_normal)
{
ai_info *aip;

Expand Down Expand Up @@ -15955,7 +15955,7 @@ void big_ship_collide_recover_start(object *objp, object *big_objp, vec3d *colli

float max_lethality = 0.0f;

void ai_update_lethality(object *pship_obj, object *other_obj, float damage)
void ai_update_lethality(const object *pship_obj, const object *other_obj, float damage)
{
// Goober5000 - stop any trickle-down errors from ship_do_damage
Assert(other_obj);
Expand Down Expand Up @@ -16008,7 +16008,7 @@ void ai_update_lethality(object *pship_obj, object *other_obj, float damage)
/**
* Object *objp_ship was hit by either weapon *objp_weapon or collided into by ship hit_objp
*/
void ai_ship_hit(object *objp_ship, object *hit_objp, vec3d *hit_normal)
void ai_ship_hit(object *objp_ship, object *hit_objp, const vec3d *hit_normal)
{
int hitter_objnum = -2;
object *objp_hitter = nullptr;
Expand Down
15 changes: 8 additions & 7 deletions code/debris/debris.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ MONITOR(NumHullDebris)
* @param exp_force Explosion force, used to assign velocity to pieces. 1.0f assigns velocity like before. 2.0f assigns twice as much to non-inherited part of velocity
* @param source_subsys The subsystem this debris came from, if any
*/
object *debris_create(object *source_obj, int model_num, int submodel_num, vec3d *pos, vec3d *exp_center, bool hull_flag, float exp_force, ship_subsys* source_subsys)
object *debris_create(object *source_obj, int model_num, int submodel_num, const vec3d *pos, const vec3d *exp_center, bool hull_flag, float exp_force, const ship_subsys* source_subsys)
{
int parent_objnum;
object *obj;
Expand All @@ -401,7 +401,7 @@ object *debris_create(object *source_obj, int model_num, int submodel_num, vec3d
return obj;
}

object *debris_create_only(int parent_objnum, int parent_ship_class, int alt_type_index, int team, float hull_strength, int spark_timeout, int model_num, int submodel_num, vec3d *pos, matrix *orient, bool hull_flag, bool vaporize, int damage_type_idx)
object *debris_create_only(int parent_objnum, int parent_ship_class, int alt_type_index, int team, float hull_strength, int spark_timeout, int model_num, int submodel_num, const vec3d *pos, const matrix *orient, bool hull_flag, bool vaporize, int damage_type_idx)
{
int objnum;
object *obj;
Expand Down Expand Up @@ -666,7 +666,7 @@ object *debris_create_only(int parent_objnum, int parent_ship_class, int alt_typ
return obj;
}

void debris_create_set_velocity(debris *db, ship *source_shipp, vec3d *exp_center, float exp_force, ship_subsys* source_subsys)
void debris_create_set_velocity(const debris *db, const ship *source_shipp, const vec3d *exp_center, float exp_force, const ship_subsys* source_subsys)
{
auto obj = &Objects[db->objnum];
auto source_obj = (source_shipp == nullptr) ? nullptr : &Objects[source_shipp->objnum];
Expand Down Expand Up @@ -1105,7 +1105,7 @@ int debris_check_collision(object *pdebris, object *other_obj, vec3d *hitpos, co
/**
* Return the team field for a debris object
*/
int debris_get_team(object *objp)
int debris_get_team(const object *objp)
{
Assert( objp->type == OBJ_DEBRIS );
Assert( objp->instance >= 0 && objp->instance < (int)Debris.size() );
Expand Down Expand Up @@ -1207,17 +1207,18 @@ void debris_render(object * obj, model_draw_list *scene)
}
}

bool debris_is_generic(debris *db)
bool debris_is_generic(const debris *db)
{
return db->model_num == Debris_model;
}

bool debris_is_vaporized(debris *db)
bool debris_is_vaporized(const debris *db)
{
return db->model_num == Debris_vaporize_model;
}

void create_generic_debris(object* ship_objp, vec3d* pos, float min_num_debris, float max_num_debris, float speed_mult, bool use_ship_debris) {
void create_generic_debris(object* ship_objp, const vec3d* pos, float min_num_debris, float max_num_debris, float speed_mult, bool use_ship_debris)
{
Assertion(ship_objp->type == OBJ_SHIP, "create_generic_debris called for a non-ship, only ships can spew debris!");
if (ship_objp->type != OBJ_SHIP)
return;
Expand Down
14 changes: 7 additions & 7 deletions code/debris/debris.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,29 @@ void debris_delete( object * obj );
void debris_process_post( object * obj, float frame_time);

// Create debris, set velocity, and fire scripting hook
object *debris_create( object *source_obj, int model_num, int submodel_num, vec3d *pos, vec3d *exp_center, bool hull_flag, float exp_force, ship_subsys* source_subsys = nullptr );
object *debris_create(object *source_obj, int model_num, int submodel_num, const vec3d *pos, const vec3d *exp_center, bool hull_flag, float exp_force, const ship_subsys* source_subsys = nullptr);

// Create debris but don't do anything else
object *debris_create_only(int parent_objnum, int parent_ship_class, int alt_type_index, int team, float hull_strength, int spark_timeout, int model_num, int submodel_num, vec3d *pos, matrix *orient, bool hull_flag, bool vaporize, int damage_type_idx);
object *debris_create_only(int parent_objnum, int parent_ship_class, int alt_type_index, int team, float hull_strength, int spark_timeout, int model_num, int submodel_num, const vec3d *pos, const matrix *orient, bool hull_flag, bool vaporize, int damage_type_idx);

// Set velocity after debris creation
void debris_create_set_velocity(debris *db, ship *source_shipp, vec3d *exp_center, float exp_force, ship_subsys* source_subsys = nullptr);
void debris_create_set_velocity(const debris *db, const ship *source_shipp, const vec3d *exp_center, float exp_force, const ship_subsys* source_subsys = nullptr);

// Fire scripting hook after debris creation
void debris_create_fire_hook(object *obj, object *source_obj);

int debris_check_collision( object * obj, object * other_obj, vec3d * hitpos, collision_info_struct *debris_hit_info=NULL, vec3d* hitnormal = NULL );
void debris_hit( object * debris_obj, object * other_obj, vec3d * hitpos, float damage, vec3d* force );
int debris_get_team(object *objp);

void debris_add_to_hull_list(debris *db);
void debris_remove_from_hull_list(debris *db);

bool debris_is_generic(debris *db);
bool debris_is_vaporized(debris *db);
int debris_get_team(const object *objp);
bool debris_is_generic(const debris *db);
bool debris_is_vaporized(const debris *db);

// creates a burst of generic debris at hitpos from ship_objp, with a random number between min and max
// use_ship_debris is for whether the ship's generic debris should be used, or simply debris01.pof
void create_generic_debris(object* ship_objp, vec3d* hitpos, float min_num_debris, float max_num_debris, float speed_mult, bool use_ship_debris);
void create_generic_debris(object* ship_objp, const vec3d* hitpos, float min_num_debris, float max_num_debris, float speed_mult, bool use_ship_debris);

#endif // _DEBRIS_H
2 changes: 1 addition & 1 deletion code/decals/decals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ void renderAll() {
draw_list.render();
}

void addDecal(creation_info& info, object* host, int submodel, const vec3d& local_pos, const matrix& local_orient) {
void addDecal(creation_info& info, const object* host, int submodel, const vec3d& local_pos, const matrix& local_orient) {
if (!Decal_system_active || !Decal_option_active) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion code/decals/decals.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void renderAll();
* orientation should look along the direction
*/
void addDecal(creation_info& info,
object* host,
const object* host,
int submodel,
const vec3d& local_pos,
const matrix& local_orient);
Expand Down
2 changes: 1 addition & 1 deletion code/hud/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef struct hud_subsys_damage
{
int str;
int type;
char* name;
const char* name;
} hud_subsys_damage;

// used for hudtarget
Expand Down
5 changes: 3 additions & 2 deletions code/hud/hudartillery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ void ssm_init()
validate_SSM_entries();
}

void ssm_get_random_start_pos(vec3d *out, vec3d *start, matrix *orient, size_t ssm_index)
void ssm_get_random_start_pos(vec3d *out, const vec3d *start, const matrix *orient, size_t ssm_index)
{
vec3d temp;
ssm_info *s = &Ssm_info[ssm_index];
Expand Down Expand Up @@ -305,7 +305,8 @@ void ssm_level_init()
}

// start a subspace missile effect
void ssm_create(object *target, vec3d *start, size_t ssm_index, ssm_firing_info *override, int team)
// (it might be possible to make `target` const, but that would set off another const-cascade)
void ssm_create(object *target, const vec3d *start, size_t ssm_index, const ssm_firing_info *override, int team)
{
ssm_strike ssm;
matrix dir;
Expand Down
3 changes: 2 additions & 1 deletion code/hud/hudartillery.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ void hud_artillery_update();
void hud_artillery_render();

// start a subspace missile effect
void ssm_create(object *target, vec3d *start, size_t ssm_index, ssm_firing_info *override, int team);
// (it might be possible to make `target` const, but that would set off another const-cascade)
void ssm_create(object *target, const vec3d *start, size_t ssm_index, const ssm_firing_info *override, int team);

// Goober5000
extern int ssm_info_lookup(const char *name);
Expand Down
6 changes: 3 additions & 3 deletions code/hud/hudescort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ HudGauge(HUD_OBJECT_ESCORT, HUD_ESCORT_VIEW, false, false, (VM_EXTERNAL | VM_DEA
{
}

void HudGaugeEscort::initHeaderText(char *text)
void HudGaugeEscort::initHeaderText(const char *text)
{
strcpy_s(header_text, text);
}
Expand Down Expand Up @@ -135,7 +135,7 @@ void HudGaugeEscort::initRightAlignNames(bool align)
right_align_names = align;
}

void HudGaugeEscort::initBitmaps(char *fname_top, char *fname_middle, char *fname_bottom)
void HudGaugeEscort::initBitmaps(const char *fname_top, const char *fname_middle, const char *fname_bottom)
{
Escort_gauges[0].first_frame = bm_load_animation(fname_top, &Escort_gauges[0].num_frames);
if (Escort_gauges[0].first_frame == -1) {
Expand Down Expand Up @@ -885,7 +885,7 @@ void hud_remove_ship_from_escort(int objnum)
* @param objp The object hit
* @param quadrant Shield quadrant on the object that was hit, alternatively -1 if no shield
*/
void hud_escort_ship_hit(object *objp, int /*quadrant*/)
void hud_escort_ship_hit(const object *objp, int /*quadrant*/)
{
// no ships on the escort list in multiplayer dogfight
if(MULTI_DOGFIGHT){
Expand Down
6 changes: 3 additions & 3 deletions code/hud/hudescort.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void hud_setup_escort_list(int level = 1);
void hud_resort_escort_list();
void hud_add_remove_ship_escort(int objnum, int supress_feedback = 0);
void hud_escort_clear_all(bool clear_flags = false);
void hud_escort_ship_hit(object *objp, int quadrant);
void hud_escort_ship_hit(const object *objp, int quadrant);
void hud_escort_target_next();
void hud_escort_cull_list();
void hud_add_ship_to_escort(int objnum, int supress_feedback);
Expand Down Expand Up @@ -55,8 +55,8 @@ class HudGaugeEscort: public HudGauge
bool right_align_names; // whether or not to right-align ship names
public:
HudGaugeEscort();
void initBitmaps(char *fname_top, char *fname_middle, char *fname_bottom);
void initHeaderText(char *text);
void initBitmaps(const char *fname_top, const char *fname_middle, const char *fname_bottom);
void initHeaderText(const char *text);
void initHeaderTextOffsets(int x, int y);
void initListStartOffsets(int x, int y);
void initEntryHeight(int h);
Expand Down
22 changes: 11 additions & 11 deletions code/hud/hudshield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ bool shield_ani_warning_displayed_already = false;

// called at beginning of level to page in all ship icons
// used in this level
void hud_ship_icon_page_in(ship_info *sip)
void hud_ship_icon_page_in(const ship_info *sip)
{
hud_frames *sgp;

Expand Down Expand Up @@ -287,7 +287,7 @@ void hud_augment_shield_quadrant(object *objp, int direction)
// Try to find a match between filename and the names inside
// of Hud_shield_filenames. This will provide us with an
// association of ship class to shield icon information.
void hud_shield_assign_info(ship_info *sip, char *filename)
void hud_shield_assign_info(ship_info *sip, const char *filename)
{
ubyte i;

Expand All @@ -300,10 +300,10 @@ void hud_shield_assign_info(ship_info *sip, char *filename)

//No HUD icon found. Add one!
sip->shield_icon_index = (unsigned char) Hud_shield_filenames.size();
Hud_shield_filenames.push_back((SCP_string)filename);
Hud_shield_filenames.emplace_back(filename);
}

void hud_show_mini_ship_integrity(object *objp, int x_force, int y_force)
void hud_show_mini_ship_integrity(const object *objp, int x_force, int y_force)
{
char text_integrity[64];
int numeric_integrity;
Expand Down Expand Up @@ -350,7 +350,7 @@ void hud_show_mini_ship_integrity(object *objp, int x_force, int y_force)

// Draw the miniature shield icon that is drawn near the reticle
// this function is only used by multi_ingame_join_display_ship() in multi_ingame.cpp as of the new HudGauge implementation (Swifty)
void hud_shield_show_mini(object *objp, int x_force, int y_force, int x_hull_offset, int y_hull_offset)
void hud_shield_show_mini(const object *objp, int x_force, int y_force, int x_hull_offset, int y_hull_offset)
{
float max_shield;
int hud_color_index, range, frame_offset;
Expand Down Expand Up @@ -426,7 +426,7 @@ void hud_shield_show_mini(object *objp, int x_force, int y_force, int x_hull_off
// reset the shield_hit_info data structure
// pass NULL as objp if you only need to initialize a shield_hit_info without an
// associated ship
void shield_info_reset(object *objp, shield_hit_info *shi)
void shield_info_reset(const object *objp, shield_hit_info *shi)
{
int n_quadrants = (objp != NULL) ? objp->n_quadrants : 0;

Expand All @@ -451,7 +451,7 @@ void shield_info_reset(object *objp, shield_hit_info *shi)
// input: player => optional parameter (default value 0). This is to indicate that player shield hit
// info should be reset. This is normally not the case.
// is for the player's current target
void hud_shield_hit_reset(object *objp, int player)
void hud_shield_hit_reset(const object *objp, int player)
{
shield_hit_info *shi;

Expand Down Expand Up @@ -495,7 +495,7 @@ void hud_shield_hit_update()
// input:
// objp => object pointer for ship that has been hit
// quadrant => quadrant of shield getting hit (-1 if no shield is present)
void hud_shield_quadrant_hit(object *objp, int quadrant)
void hud_shield_quadrant_hit(const object *objp, int quadrant)
{
shield_hit_info *shi;
int num;
Expand Down Expand Up @@ -550,7 +550,7 @@ void HudGaugeShield::render(float /*frametime*/)
{
}

void HudGaugeShield::showShields(object *objp, int mode)
void HudGaugeShield::showShields(const object *objp, int mode)
{
// static int fod_model = -1;
float max_shield;
Expand Down Expand Up @@ -924,7 +924,7 @@ void HudGaugeShieldMini::init2DigitOffsets(int x, int y)
Mini_2digit_offsets[1] = y;
}

void HudGaugeShieldMini::initBitmaps(char *fname)
void HudGaugeShieldMini::initBitmaps(const char *fname)
{
Shield_mini_gauge.first_frame = bm_load_animation(fname, &Shield_mini_gauge.num_frames);
if ( Shield_mini_gauge.first_frame == -1 ) {
Expand Down Expand Up @@ -953,7 +953,7 @@ void HudGaugeShieldMini::pageIn()
}

// Draw the miniature shield icon that is drawn near the reticle
void HudGaugeShieldMini::showMiniShields(object *objp)
void HudGaugeShieldMini::showMiniShields(const object *objp)
{
if (Shield_mini_gauge.first_frame < 0)
return;
Expand Down
Loading

0 comments on commit ac050dd

Please sign in to comment.