Skip to content

Commit beaeafe

Browse files
authored
Merge pull request #7086 from Goober5000/vectorize_subsys_hits
vectorize subsystem damage/heal list
2 parents a243052 + d77f413 commit beaeafe

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

code/ship/shiphit.cpp

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,6 @@ static float subsys_get_range(const object *other_obj, const ship_subsys *subsys
435435
return range;
436436
}
437437

438-
#define MAX_SUBSYS_LIST 200 //DTP MAX SUBSYS LIST BUMPED FROM 32 to 200, ahmm 32???
439-
440438
typedef struct {
441439
float dist;
442440
float range;
@@ -452,7 +450,7 @@ std::pair<std::optional<ConditionData>, float> do_subobj_heal_stuff(const object
452450
float healing_left;
453451
int weapon_info_index;
454452
ship* ship_p;
455-
sublist subsys_list[MAX_SUBSYS_LIST];
453+
SCP_vector<sublist> subsys_list;
456454
int subsys_hit_first = -1; // the subsys which should be hit first and take most of the healing; index into subsys_list
457455
vec3d hitpos2;
458456

@@ -476,8 +474,7 @@ std::pair<std::optional<ConditionData>, float> do_subobj_heal_stuff(const object
476474

477475
// First, create a list of the N subsystems within range.
478476
// Then, one at a time, process them in order.
479-
int count = 0;
480-
for (auto subsys = GET_FIRST(&ship_p->subsys_list); subsys != END_OF_LIST(&ship_p->subsys_list); subsys = GET_NEXT(subsys))
477+
for (auto subsys : list_range(&ship_p->subsys_list))
481478
{
482479
model_subsystem* mss = subsys->system_info;
483480

@@ -510,7 +507,7 @@ std::pair<std::optional<ConditionData>, float> do_subobj_heal_stuff(const object
510507
if (Damage_impacted_subsystem_first && submodel_num != -1 && (submodel_num == mss->subobj_num || submodel_num == mss->turret_gun_sobj)) {
511508
// If the hit impacted this subsystem's submodel, then make sure this subsys
512509
// gets healed first, even if another subsystem is closer to the hit location
513-
subsys_hit_first = count;
510+
subsys_hit_first = sz2i(subsys_list.size());
514511
}
515512

516513
if (mss->flags[Model::Subsystem_Flags::Collide_submodel]) {
@@ -521,14 +518,7 @@ std::pair<std::optional<ConditionData>, float> do_subobj_heal_stuff(const object
521518
}
522519
}
523520

524-
subsys_list[count].dist = dist;
525-
subsys_list[count].range = range;
526-
subsys_list[count].ptr = subsys;
527-
count++;
528-
529-
if (count >= MAX_SUBSYS_LIST) {
530-
break;
531-
}
521+
subsys_list.push_back({ dist, range, subsys });
532522
}
533523
}
534524
}
@@ -552,8 +542,8 @@ std::pair<std::optional<ConditionData>, float> do_subobj_heal_stuff(const object
552542
// Now scan the sorted list of subsystems in range.
553543
// Apply healing to the nearest one first (exception: subsys_hit_first),
554544
// subtracting off healing as we go.
555-
int i, j;
556-
for (j = 0; j < count; j++)
545+
int count = sz2i(subsys_list.size());
546+
for (int j = 0; j < count; j++)
557547
{
558548
float dist, range;
559549
ship_subsys* subsystem;
@@ -563,7 +553,7 @@ std::pair<std::optional<ConditionData>, float> do_subobj_heal_stuff(const object
563553
float min_dist = 9999999.9f;
564554

565555
// find the closest subsystem
566-
for (i=0; i<count; i++) {
556+
for (int i=0; i<count; i++) {
567557
if (subsys_list[i].dist < min_dist) {
568558
min_dist = subsys_list[i].dist;
569559
min_index = i;
@@ -666,6 +656,7 @@ std::pair<std::optional<ConditionData>, float> do_subobj_heal_stuff(const object
666656
break;
667657
}
668658
}
659+
669660
return std::make_pair(subsys_impact, healing);
670661
}
671662

@@ -709,7 +700,7 @@ std::pair<std::optional<ConditionData>, float> do_subobj_hit_stuff(object *ship_
709700
float damage_left, damage_if_hull;
710701
int weapon_info_index;
711702
ship *ship_p;
712-
sublist subsys_list[MAX_SUBSYS_LIST];
703+
SCP_vector<sublist> subsys_list;
713704
int subsys_hit_first = -1; // the subsys which should be hit first and take most of the damage; index into subsys_list
714705
vec3d hitpos2;
715706
float ss_dif_scale = 1.0f; // Nuke: Set a base dificulty scale for compatibility
@@ -808,8 +799,7 @@ std::pair<std::optional<ConditionData>, float> do_subobj_hit_stuff(object *ship_
808799

809800
// First, create a list of the N subsystems within range.
810801
// Then, one at a time, process them in order.
811-
int count = 0;
812-
for ( auto subsys=GET_FIRST(&ship_p->subsys_list); subsys != END_OF_LIST(&ship_p->subsys_list); subsys = GET_NEXT(subsys) )
802+
for (auto subsys : list_range(&ship_p->subsys_list))
813803
{
814804
model_subsystem *mss = subsys->system_info;
815805

@@ -864,7 +854,7 @@ std::pair<std::optional<ConditionData>, float> do_subobj_hit_stuff(object *ship_
864854
if (Damage_impacted_subsystem_first && submodel_num != -1 && (submodel_num == mss->subobj_num || submodel_num == mss->turret_gun_sobj)) {
865855
// If the hit impacted this subsystem's submodel, then make sure this subsys
866856
// gets dealt damage first, even if another subsystem is closer to the hit location
867-
subsys_hit_first = count;
857+
subsys_hit_first = sz2i(subsys_list.size());
868858
}
869859

870860
if (mss->flags[Model::Subsystem_Flags::Collide_submodel]) {
@@ -875,14 +865,7 @@ std::pair<std::optional<ConditionData>, float> do_subobj_hit_stuff(object *ship_
875865
}
876866
}
877867

878-
subsys_list[count].dist = dist;
879-
subsys_list[count].range = range;
880-
subsys_list[count].ptr = subsys;
881-
count++;
882-
883-
if (count >= MAX_SUBSYS_LIST){
884-
break;
885-
}
868+
subsys_list.push_back({ dist, range, subsys });
886869
}
887870
}
888871
}
@@ -913,8 +896,8 @@ std::pair<std::optional<ConditionData>, float> do_subobj_hit_stuff(object *ship_
913896
// Now scan the sorted list of subsystems in range.
914897
// Apply damage to the nearest one first (exception: subsys_hit_first),
915898
// subtracting off damage as we go.
916-
int i, j;
917-
for (j=0; j<count; j++)
899+
int count = sz2i(subsys_list.size());
900+
for (int j=0; j<count; j++)
918901
{
919902
float dist, range;
920903
ship_subsys *subsystem;
@@ -924,7 +907,7 @@ std::pair<std::optional<ConditionData>, float> do_subobj_hit_stuff(object *ship_
924907
float min_dist = 9999999.9f;
925908

926909
// find the closest subsystem
927-
for (i=0; i<count; i++) {
910+
for (int i=0; i<count; i++) {
928911
if (subsys_list[i].dist < min_dist) {
929912
min_dist = subsys_list[i].dist;
930913
min_index = i;

0 commit comments

Comments
 (0)