Skip to content

Commit

Permalink
Fix bug 969 - pressing m caused segfault (vegastrike#973)
Browse files Browse the repository at this point in the history
  • Loading branch information
royfalk authored Jan 17, 2025
1 parent 871a692 commit 00b3873
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 42 deletions.
13 changes: 9 additions & 4 deletions engine/src/cmd/damageable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,21 @@ void Damageable::DamageRandomSystem(InflictedDamage inflicted_damage, bool playe
}

bool damage_system;
double current_hull = layers[0].facets[0].health;
double max_hull = layers[0].facets[0].max_health;

if (player) {
damage_system = DestroyPlayerSystem(*current_hull, *max_hull, 1);
damage_system = DestroyPlayerSystem(current_hull, max_hull, 1);
} else {
damage_system = DestroySystem(*current_hull, *max_hull, 1);
damage_system = DestroySystem(current_hull, max_hull, 1);
}

if (!damage_system) {
return;
}

unit->DamageRandSys(configuration()->physics_config.indiscriminate_system_destruction * rand01() +
(1 - configuration()->physics_config.indiscriminate_system_destruction) * (1 - ((*current_hull) > 0 ? hull_damage / (*current_hull) : 1.0f)),
(1 - configuration()->physics_config.indiscriminate_system_destruction) * (1 - ((current_hull) > 0 ? hull_damage / (current_hull) : 1.0f)),
attack_vector, 1.0f, 1.0f);
}

Expand Down Expand Up @@ -410,7 +413,9 @@ void Damageable::DamageCargo(InflictedDamage inflicted_damage) {
}

// Is the hit unit, lucky or not
if (DestroySystem(*current_hull, *max_hull, unit->numCargo())) {
double current_hull = layers[0].facets[0].health;
double max_hull = layers[0].facets[0].max_health;
if (DestroySystem(current_hull, max_hull, unit->numCargo())) {
return;
}

Expand Down
7 changes: 1 addition & 6 deletions engine/src/cmd/damageable.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class Damageable : public DamageableObject {
DamageableLayer *armor;
DamageableLayer *shield;

float *current_hull;
float *max_hull;

// These are only used for upgrade due to macros
// TODO: refactor
float upgrade_hull;
Expand All @@ -58,8 +55,6 @@ class Damageable : public DamageableObject {
Damageable() : hull(&layers[0]),
armor(&layers[1]),
shield(&layers[2]),
current_hull(&hull->facets[as_integer(FacetName::single)].health),
max_hull(&hull->facets[as_integer(FacetName::single)].max_health),
upgrade_hull(0),
shield_regeneration(0),
killed(false) {
Expand All @@ -78,7 +73,7 @@ class Damageable : public DamageableObject {
// be easily changed.
// TODO: convert all calls to *current_hull
const float GetHull() const {
return *current_hull;
return layers[0].facets[0].health;
}

// TODO: check for valid index
Expand Down
17 changes: 10 additions & 7 deletions engine/src/cmd/drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,12 @@ void Drawable::Draw(const Transformation &parent, const Matrix &parentMatrix) {
Matrix wmat;

if (!cam_setup_phase) {
double current_hull = unit->layers[0].facets[0].health;
double max_hull = unit->layers[0].facets[0].max_health;

// Following stuff is only needed in actual drawing phase
if ((*unit->current_hull) < (*unit->max_hull)) {
damagelevel = (*unit->current_hull) / (*unit->max_hull);
if ((current_hull) < (max_hull)) {
damagelevel = (current_hull) / (max_hull);
chardamage = (255 - (unsigned char) (damagelevel * 255));
}
avgscale = sqrt((ctm->getP().MagnitudeSquared() + ctm->getR().MagnitudeSquared()) * 0.5);
Expand Down Expand Up @@ -387,8 +390,8 @@ void Drawable::DrawNow(const Matrix &mato, float lod) {
}
float damagelevel = 1.0;
unsigned char chardamage = 0;
if (*unit->current_hull < *unit->max_hull) {
damagelevel = (*unit->current_hull) / (*unit->max_hull);
if (unit->layers[0].facets[0].health < unit->layers[0].facets[0].max_health) {
damagelevel = (unit->layers[0].facets[0].Percent());
chardamage = (255 - (unsigned char) (damagelevel * 255));
}
#ifdef VARIABLE_LENGTH_PQR
Expand Down Expand Up @@ -661,7 +664,7 @@ void Drawable::Sparkle(bool on_screen, Matrix *ctm) {
}

// Undamaged units don't sparkle
float damage_level = (*unit->current_hull) / (*unit->max_hull);
float damage_level = unit->layers[0].facets[0].Percent();
if (damage_level >= .99) {
return;
}
Expand Down Expand Up @@ -730,7 +733,7 @@ void Drawable::DrawHalo(bool on_screen, float apparent_size, Matrix wmat, Cloak
}

Vector Scale(1, 1, 1); //Now, HaloSystem handles that
float damage_level = (*unit->current_hull) / (*unit->max_hull);
float damage_level = unit->layers[0].facets[0].Percent();
//WARNING: cmas is not a valid maximum speed for the upcoming multi-direction thrusters,
//nor is maxaccel. Instead, each halo should have its own limits specified in units.csv
float nebd = (_Universe->AccessCamera()->GetNebula() == unit->nebula && unit->nebula != nullptr) ? -1 : 0;
Expand Down Expand Up @@ -927,7 +930,7 @@ void Drawable::Split(int level) {
tempmeshes.push_back(old[k]);
}
unit->SubUnits.prepend(splitsub = new Unit(tempmeshes, true, unit->faction));
*splitsub->current_hull = 1000.0f;
splitsub->layers[0].facets[0] = Health(0, 1000);
splitsub->name = "debris";
splitsub->setMass(game_options()->debris_mass * splitsub->getMass() / level);
splitsub->pImage->timeexplode = .1;
Expand Down
2 changes: 1 addition & 1 deletion engine/src/cmd/missile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ Missile::Missile(const char *filename,
had_target(false) {
// TODO: why would a sparkling missile be four times as hard to kill???
if (configuration()->graphics_config.missile_sparkle) {
*current_hull *= 4;
layers[0].facets[0] = Health(0, layers[0].facets[0].max_health * 4);
}
}

Expand Down
6 changes: 4 additions & 2 deletions engine/src/cmd/planet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ void Planet::InitPlanet(QVector x,
const float densityOfRock = configuration()->physics_config.density_of_rock;
const float densityOfJumpPoint = configuration()->physics_config.density_of_jump_point;
//static float massofplanet = XMLSupport::parse_float(vs_config->getVariable("physics","mass_of_planet","10000000"));
*current_hull = (4.0 / 3.0) * M_PI * radius * radius * radius * (notJumppoint ? densityOfRock : densityOfJumpPoint);
layers[0].facets[0] = Health(0, (4.0 / 3.0) * M_PI * radius * radius * radius * (notJumppoint ? densityOfRock : densityOfJumpPoint));
this->Mass =
(4.0 / 3.0) * M_PI * radius * radius * radius * (notJumppoint ? densityOfRock : (densityOfJumpPoint / 100000));
SetAI(new PlanetaryOrbit(this, vely, pos, x, y, orbitcent, parent)); //behavior
Expand Down Expand Up @@ -573,7 +573,9 @@ void Planet::AddFog(const std::vector<AtmosphericFogMesh> &v, bool opticalillusi
}
fawg->setFaceCamera();
getSubUnits().preinsert(fawg);
*fawg->current_hull /= fawg->GetHullPercent();

// TODO: this is very weird. Why are we doing this?
// fawg->hull /= fawg->GetHullPercent();
#ifdef MESHONLY
meshdata.push_back( shield );
#endif
Expand Down
2 changes: 1 addition & 1 deletion engine/src/cmd/script/script_call_unit_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ varInst *Mission::call_unit(missionNode *node, int mode) {
} else if (method_id == CMT_UNIT_getHullData) {
float res = 0.0;
if (mode == SCRIPT_RUN) {
res = *my_unit->current_hull;
res = my_unit->layers[0].facets[0].health;
}
viret = newVarInst(VI_TEMP);
viret->type = VAR_FLOAT;
Expand Down
19 changes: 0 additions & 19 deletions engine/src/cmd/unit_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2968,25 +2968,6 @@ bool Unit::UpAndDownGrade(const Unit *up,
shield->UpdateRegeneration(shield_regeneration);
}

// Upgrade hull health
upgrade_hull = *current_hull;

if (up && up->current_hull) {
const_cast<Unit *>(up)->upgrade_hull = *up->current_hull;
}

if (templ && templ->current_hull) {
const_cast<Unit *>(templ)->upgrade_hull = *templ->current_hull;
}

if (!csv_cell_null_check || force_change_on_nothing || cell_has_recursive_data(upgrade_name, up->faction, "Hull")) {
STDUPGRADE(upgrade_hull, up->upgrade_hull, templ->upgrade_hull, 0);
}

if ((hull->facets[0].max_health < hull->facets[0].health) && (!Destroyed())) {
hull->facets[0].max_health = hull->facets[0].health;
}

/*if (!csv_cell_null_check || force_change_on_nothing
|| cell_has_recursive_data(upgrade_name, up->faction, "Reactor_Recharge"))
STDUPGRADE(recharge, up->recharge, templ->recharge, 0);*/
Expand Down
3 changes: 2 additions & 1 deletion engine/src/cmd/unit_optimize_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ void UnitOptimizeFactory::RecursiveParse(std::map<std::string, std::string> unit
const boost::json::object data_object = object.at("data").as_object();

for(const boost::json::key_value_pair& pair : data_object) {
const std::string key = pair.key();
const std::string value = boost::json::value_to<std::string>(pair.value());
unit_attributes[pair.key()] = value;
unit_attributes[key] = value;
}

if(unit_attributes.count("Key")) {
Expand Down
2 changes: 1 addition & 1 deletion engine/src/cmd/unit_util_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ float PercentOperational(Unit *un, std::string name, std::string category, bool
} else if (name.find("add_") != 0 && name.find("mult_") != 0) {
float armor[8];
upgrade->ArmorData(armor);
if (*upgrade->current_hull > 1 || armor[0] || armor[1] || armor[2] || armor[3] || armor[4] || armor[5]
if (upgrade->layers[0].facets[0].health > 1 || armor[0] || armor[1] || armor[2] || armor[3] || armor[4] || armor[5]
|| armor[6]
|| armor[7]) {
if (countHullAndArmorAsFull) {
Expand Down

0 comments on commit 00b3873

Please sign in to comment.