Skip to content

Commit

Permalink
Refactor -- Renamed span cache variables and functions to be more pre…
Browse files Browse the repository at this point in the history
…cise.

- This should make understanding the code around these optimizations easier.
  • Loading branch information
Espyo committed Apr 29, 2024
1 parent 752dea9 commit 96ef590
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 37 deletions.
12 changes: 6 additions & 6 deletions Source/source/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,19 @@ animation_database::animation_database(


/**
* @brief Calculates the maximum distance that any of its hitbox can reach.,
* and stores it in the max_span variable.
* @brief Calculates the maximum distance that any of its hitbox can reach,
* and stores it in the hitbox_span variable.
*/
void animation_database::calculate_max_span() {
max_span = 0.0f;
void animation_database::calculate_hitbox_span() {
hitbox_span = 0.0f;
for(size_t s = 0; s < sprites.size(); ++s) {
sprite* s_ptr = sprites[s];
for(size_t h = 0; h < s_ptr->hitboxes.size(); ++h) {
hitbox* h_ptr = &s_ptr->hitboxes[h];

float d = dist(point(0, 0), h_ptr->pos).to_float();
d += h_ptr->radius;
max_span = std::max(max_span, d);
hitbox_span = std::max(hitbox_span, d);
}
}
}
Expand Down Expand Up @@ -550,7 +550,7 @@ void animation_database::load_from_data_node(data_node* node) {
}

//Finish up.
calculate_max_span();
calculate_hitbox_span();
}


Expand Down
4 changes: 2 additions & 2 deletions Source/source/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class animation_database : public content {
vector<size_t> pre_named_conversions;

//Maximum span of the hitboxes. Cache for performance.
float max_span = 0.0f;
float hitbox_span = 0.0f;


//--- Function declarations ---
Expand All @@ -269,7 +269,7 @@ class animation_database : public content {
size_t find_animation(const string &name) const;
size_t find_sprite(const string &name) const;
size_t find_body_part(const string &name) const;
void calculate_max_span();
void calculate_hitbox_span();
void create_conversions(
vector<std::pair<size_t, string> > conversions, const data_node* file
);
Expand Down
4 changes: 2 additions & 2 deletions Source/source/game_states/gameplay/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void gameplay_state::do_aesthetic_leader_logic(const float delta_t) {
throw_dest_mob = nullptr;
for(size_t m = 0; m < mobs.all.size(); ++m) {
mob* m_ptr = mobs.all[m];
if(!bbox_check(throw_dest, m_ptr->pos, m_ptr->max_span)) {
if(!bbox_check(throw_dest, m_ptr->pos, m_ptr->physical_span)) {
//Too far away; of course the cursor isn't on it.
continue;
}
Expand Down Expand Up @@ -1503,7 +1503,7 @@ void gameplay_state::process_mob_interactions(mob* m_ptr, size_t m) {
game.perf_mon->start_measurement("Objects -- Touching others");
}

if(d <= m_ptr->max_span + m2_ptr->max_span) {
if(d <= m_ptr->physical_span + m2_ptr->physical_span) {
//Only check if their radii or hitboxes
//can (theoretically) reach each other.
process_mob_touches(m_ptr, m2_ptr, m, m2, d);
Expand Down
8 changes: 4 additions & 4 deletions Source/source/mob_types/mob_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,10 +884,10 @@ void mob_type::load_from_data_node(
anims.create_conversions(get_anim_conversions(), node);
}

max_span =
calculate_mob_max_span(
physical_span =
calculate_mob_physical_span(
radius,
(load_resources ? anims.max_span : 0),
(load_resources ? anims.hitbox_span : 0),
rectangular_dim
);

Expand Down Expand Up @@ -946,7 +946,7 @@ void create_special_mob_types() {
bridge_component_type->appears_in_area_editor = false;
bridge_component_type->casts_shadow = false;
bridge_component_type->height = 8.0f;
bridge_component_type->max_span = 8.0f;
bridge_component_type->physical_span = 8.0f;
bridge_component_type->radius = 8.0f;
bridge_component_type->walkable = true;
bridge_component_type->draw_mob_callback = bridge::draw_component;
Expand Down
5 changes: 3 additions & 2 deletions Source/source/mob_types/mob_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,9 @@ class mob_type : public content {

//- Caches -

//How far its hitboxes or radius can reach from the center.
float max_span = 0.0f;
//How far its radius or hitboxes reach from the center.
//Cache for performance.
float physical_span = 0.0f;


//--- Function declarations ---
Expand Down
18 changes: 9 additions & 9 deletions Source/source/mobs/mob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ mob::mob(const point &pos, mob_type* type, const float angle) :
max_health(type->max_health),
itch_time(type->itch_time),
anim(&type->anims),
max_span(type->max_span) {
physical_span(type->physical_span) {

game.states.gameplay->next_mob_id++;

Expand Down Expand Up @@ -2422,8 +2422,8 @@ bool mob::has_clear_line(const mob* target_mob) const {
if(
!rectangles_intersect(
bb_tl, bb_br,
m_ptr->pos - m_ptr->max_span,
m_ptr->pos + m_ptr->max_span
m_ptr->pos - m_ptr->physical_span,
m_ptr->pos + m_ptr->physical_span
)
) {
continue;
Expand Down Expand Up @@ -2980,10 +2980,10 @@ void mob::set_health(const bool add, const bool ratio, const float amount) {
*/
void mob::set_radius(const float radius) {
this->radius = radius;
max_span =
calculate_mob_max_span(
physical_span =
calculate_mob_physical_span(
radius,
type->anims.max_span,
type->anims.hitbox_span,
rectangular_dim
);
}
Expand All @@ -2996,10 +2996,10 @@ void mob::set_radius(const float radius) {
*/
void mob::set_rectangular_dim(const point &rectangular_dim) {
this->rectangular_dim = rectangular_dim;
max_span =
calculate_mob_max_span(
physical_span =
calculate_mob_physical_span(
radius,
type->anims.max_span,
type->anims.hitbox_span,
rectangular_dim
);
}
Expand Down
5 changes: 3 additions & 2 deletions Source/source/mobs/mob.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,9 @@ class mob {
//Cached value of the angle's sine.
float angle_sin = 0.0f;

//Cached value of how far its hitboxes or radius can reach from the center.
float max_span = 0.0f;
//How far its radius or hitboxes reach from the center.
//Cache for performance.
float physical_span = 0.0f;

//It's invisible due to a status effect. Cache for performance.
bool has_invisibility_status = false;
Expand Down
18 changes: 10 additions & 8 deletions Source/source/mobs/mob_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,26 +1058,28 @@ track_t::track_t(


/**
* @brief Calculates the maximum span that a mob can ever reach from its center.
* @brief Calculates the maximum physical span that a mob can ever reach
* from its center.
*
* @param radius The mob's radius.
* @param anim_max_span Maximum span of its animation-related data.
* @param anim_hitbox_span Maximum span of its hitboxes data.
* @param rectangular_dim Rectangular dimensions of the mob, if any.
* @return The span.
*/
float calculate_mob_max_span(
const float radius, const float anim_max_span, const point &rectangular_dim
float calculate_mob_physical_span(
const float radius, const float anim_hitbox_span,
const point &rectangular_dim
) {
float max_span = std::max(radius, anim_max_span);
float final_span = std::max(radius, anim_hitbox_span);

if(rectangular_dim.x != 0) {
max_span =
final_span =
std::max(
max_span, dist(point(0, 0), rectangular_dim / 2.0).to_float()
final_span, dist(point(0, 0), rectangular_dim / 2.0).to_float()
);
}

return max_span;
return final_span;
}


Expand Down
5 changes: 3 additions & 2 deletions Source/source/mobs/mob_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,9 @@ struct track_t {
};


float calculate_mob_max_span(
const float radius, const float anim_max_span, const point &rectangular_dim
float calculate_mob_physical_span(
const float radius, const float anim_hitbox_span,
const point &rectangular_dim
);
mob* create_mob(
mob_category* category, const point &pos, mob_type* type,
Expand Down

0 comments on commit 96ef590

Please sign in to comment.