Skip to content

Commit

Permalink
Implement cloak as a component (#872)
Browse files Browse the repository at this point in the history
- move cloak class to component folder
- move some python functions from energetic to unit class in preparation to delete it
- other callers to use the component functions
  • Loading branch information
royfalk authored Sep 1, 2024
1 parent bff6ff3 commit cbcae9d
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 144 deletions.
3 changes: 2 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ SET(LIBCOMPONENT
src/components/energy_consumer.cpp
src/components/energy_container.cpp
src/components/reactor.cpp

src/components/cloak.cpp
)

SET(LIBGUI_SOURCES
Expand Down Expand Up @@ -900,7 +902,6 @@ SET(LIBCMD_SOURCES

src/cmd/intelligent.cpp
src/cmd/energetic.cpp
src/cmd/cloak.cpp

src/cmd/planetary_orbit.cpp

Expand Down
2 changes: 1 addition & 1 deletion engine/src/cmd/ai/firekeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2178,7 +2178,7 @@ void FireKeyboard::Execute() {
} //use specialized ejectdock in the future
}
static bool actually_arrest = XMLSupport::parse_bool(vs_config->getVariable("AI", "arrest_energy_zero", "false"));
if (actually_arrest && parent->energyRechargeData() == 0) {
if (actually_arrest && parent->reactor.Capacity() == 0) {
Arrested(parent);
}
}
Expand Down
28 changes: 14 additions & 14 deletions engine/src/cmd/basecomputer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5184,7 +5184,7 @@ void showUnitStats(Unit *playerUnit, string &text, int subunitlevel, int mode, C
if (shields_require_power) {
maxshield = 0;
}
PRETTY_ADDU(statcolor + "Recharge: #-c", playerUnit->energyRechargeData() * RSconverter, 0, "MJ/s");
PRETTY_ADDU(statcolor + "Recharge: #-c", playerUnit->reactor.Capacity() * RSconverter, 0, "MJ/s");
PRETTY_ADDU(statcolor + "Weapon capacitor bank storage: #-c",
((playerUnit->maxEnergyData() - maxshield) * RSconverter), 0, "MJ");
//note: I found no function to get max warp energy, but since we're docked they are the same
Expand Down Expand Up @@ -5223,9 +5223,9 @@ void showUnitStats(Unit *playerUnit, string &text, int subunitlevel, int mode, C
} else {
switch (replacement_mode) {
case 0: //Replacement or new Module
if (MODIFIES(replacement_mode, playerUnit, blankUnit, energyRechargeData()))
if (MODIFIES(replacement_mode, playerUnit, blankUnit, reactor.Capacity()))
PRETTY_ADDU(statcolor + "Installs reactor with recharge rate: #-c",
playerUnit->energyRechargeData() * RSconverter, 0, "MJ/s");
playerUnit->reactor.Capacity() * RSconverter, 0, "MJ/s");
if (MODIFIES(replacement_mode, playerUnit, blankUnit, maxEnergyData()))
PRETTY_ADDU(statcolor + "Installs main capacitor bank with storage capacity: #-c",
(playerUnit->maxEnergyData() * RSconverter), 0, "MJ");
Expand All @@ -5238,9 +5238,9 @@ void showUnitStats(Unit *playerUnit, string &text, int subunitlevel, int mode, C
}
break;
case 1: //Additive
if (MODIFIES(replacement_mode, playerUnit, blankUnit, energyRechargeData()))
if (MODIFIES(replacement_mode, playerUnit, blankUnit, reactor.Capacity()))
PRETTY_ADDU(statcolor + "Increases recharge rate by #-c",
playerUnit->energyRechargeData() * RSconverter, 0, "MJ/s");
playerUnit->reactor.Capacity() * RSconverter, 0, "MJ/s");
if (MODIFIES(replacement_mode, playerUnit, blankUnit, maxEnergyData()))
PRETTY_ADDU(statcolor + "Adds #-c",
(playerUnit->maxEnergyData() * RSconverter),
Expand All @@ -5253,9 +5253,9 @@ void showUnitStats(Unit *playerUnit, string &text, int subunitlevel, int mode, C
"MJ of storage to warp capacitor bank");
break;
case 2: //multiplicative
if (MODIFIES(replacement_mode, playerUnit, blankUnit, energyRechargeData()))
if (MODIFIES(replacement_mode, playerUnit, blankUnit, reactor.Capacity()))
PRETTY_ADDU(statcolor + "Increases reactor recharge rate by #-c",
100.0 * (playerUnit->energyRechargeData() - 1), 0, "%");
100.0 * (playerUnit->reactor.Capacity() - 1), 0, "%");
if (MODIFIES(replacement_mode, playerUnit, blankUnit, maxEnergyData()))
PRETTY_ADDU(statcolor + "Increases main capacitor bank storage by #-c",
100.0 * (playerUnit->maxEnergyData() - 1), 0, "%");
Expand Down Expand Up @@ -5432,14 +5432,14 @@ void showUnitStats(Unit *playerUnit, string &text, int subunitlevel, int mode, C
if (playerUnit->cloak.Capable()) {
if (!mode) {
PRETTY_ADDU(statcolor + "Cloaking device available, energy usage: #-c",
playerUnit->cloak.Energy() * RSconverter * Wconv,
playerUnit->cloak.GetConsumption() * RSconverter * Wconv,
0,
"MJ/s");
} else {
switch (replacement_mode) {
case 0: //Replacement or new Module
PRETTY_ADDU(statcolor + "Installs a cloaking device.#n# Activated energy usage: #-c",
playerUnit->cloak.Energy() * RSconverter * Wconv,
playerUnit->cloak.GetConsumption() * RSconverter * Wconv,
0,
"MJ/s");
break;
Expand Down Expand Up @@ -5616,7 +5616,7 @@ void showUnitStats(Unit *playerUnit, string &text, int subunitlevel, int mode, C
float overhead = (shields_require_power) ?
(regeneration / shieldenergycap * shield_maintenance_cost
* num_shields * VSDM) : 0;
float nrt = avail / (playerUnit->energyRechargeData() * RSconverter); // TODO -overhead);
float nrt = avail / (playerUnit->reactor.Capacity() * RSconverter); // TODO -overhead);
PRETTY_ADDU(statcolor + "Reactor nominal replenish time: #-c", nrt, 2, "seconds");
//shield related stuff
//code taken from RegenShields in unit_generic.cpp, so we're sure what we say here is correct.
Expand All @@ -5634,7 +5634,7 @@ void showUnitStats(Unit *playerUnit, string &text, int subunitlevel, int mode, C
}

if (num_shields) {
if (regeneration * num_shields * VSDM / shieldenergycap > playerUnit->energyRechargeData()
if (regeneration * num_shields * VSDM / shieldenergycap > playerUnit->reactor.Capacity()
* RSconverter) {
text += "#n##c1:1:.1#" + prefix
+ "WARNING: reactor recharge rate is less than combined shield recharge rate.#n#";
Expand All @@ -5643,7 +5643,7 @@ void showUnitStats(Unit *playerUnit, string &text, int subunitlevel, int mode, C
if (shields_require_power) {
text += "#n#" + prefix + statcolor + "Reactor recharge slowdown caused by shield maintenance: #-c";
float maint_draw_percent = regeneration * VSDM * 100.0 / shieldenergycap * shield_maintenance_cost
* num_shields / (playerUnit->energyRechargeData() * RSconverter);
* num_shields / (playerUnit->reactor.Capacity() * RSconverter);
text += (boost::format("%1$.2f") % maint_draw_percent).str();
text += " %.";
if (maint_draw_percent > 60) {
Expand All @@ -5662,14 +5662,14 @@ void showUnitStats(Unit *playerUnit, string &text, int subunitlevel, int mode, C
float maint_draw =
(shields_require_power && num_shields) ? (regeneration * VSDM / shieldenergycap
* shield_maintenance_cost * num_shields) : 0;
if (totalWeaponEnergyUsage < (playerUnit->energyRechargeData() * RSconverter - maint_draw)) {
if (totalWeaponEnergyUsage < (playerUnit->reactor.Capacity() * RSconverter - maint_draw)) {
//waouh, impressive...
text += "#n##c0:1:.2#" + prefix + "Your reactor produces more energy than your weapons can use!#-c";
} else {
PRETTY_ADDU(statcolor + "Reactor energy depletion time if weapons in continuous use: #-c",
(playerUnit->maxEnergyData()
* RSconverter) / (totalWeaponEnergyUsage
- ((playerUnit->energyRechargeData() * RSconverter - maint_draw))),
- ((playerUnit->reactor.Capacity() * RSconverter - maint_draw))),
2,
"seconds");
}
Expand Down
24 changes: 1 addition & 23 deletions engine/src/cmd/energetic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,7 @@ void Energetic::DecreaseWarpEnergyInWarp() {
}
}

float Energetic::energyData() const {
const Unit *unit = vega_dynamic_cast_ptr<const Unit>(this);
float capacitance = const_cast<Energetic *>(this)->totalShieldEnergyCapacitance();

if (configuration()->physics_config.max_shield_lowers_capacitance) {
if (unit->energy.MaxLevel() <= capacitance) {
return 0;
}
return (unit->energy.Level()) / (unit->energy.MaxLevel() - capacitance);
} else {
return unit->energy.Percent();
}
}

float Energetic::energyRechargeData() const {
const Unit *unit = vega_dynamic_cast_ptr<const Unit>(this);
return unit->reactor.Capacity();
}

float Energetic::fuelData() const {
const Unit *unit = vega_dynamic_cast_ptr<const Unit>(this);
return unit->fuel.Level();
}

float Energetic::getFuelUsage(bool afterburner) {
if (afterburner) {
Expand Down Expand Up @@ -236,7 +214,7 @@ float Energetic::warpEnergyData() const {
}

// Basically max or current shield x 0.2
float Energetic::totalShieldEnergyCapacitance() {
float Energetic::totalShieldEnergyCapacitance() const {
const Unit *unit = vega_dynamic_cast_ptr<const Unit>(this);
DamageableLayer *shield = unit->shield;

Expand Down
6 changes: 1 addition & 5 deletions engine/src/cmd/energetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ class Energetic {
void decreaseWarpEnergy(bool insys, double time);
void DecreaseWarpEnergyInWarp();

float energyData() const;
float energyRechargeData() const;

float fuelData() const;

static float getFuelUsage(bool afterburner);
void WCWarpIsFuelHack(bool transfer_warp_to_fuel);
Expand All @@ -65,7 +61,7 @@ class Energetic {
void setAfterburnerEnergy(float aft);
void setEnergyRecharge(float enrech);

float totalShieldEnergyCapacitance();
float totalShieldEnergyCapacitance() const;

static float VSDPercent();

Expand Down
28 changes: 14 additions & 14 deletions engine/src/cmd/movable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void Movable::UpdatePhysics(const Transformation &trans,
}

void Movable::AddVelocity(float difficulty) {
Unit *unit = static_cast<Unit *>(this);
const Unit *unit = vega_dynamic_const_cast_ptr<const Unit>(this);
float lastWarpField = graphicOptions.WarpFieldStrength;

bool playa = isPlayerShip();
Expand Down Expand Up @@ -474,8 +474,8 @@ double Movable::GetMaxWarpFieldStrength(float rampmult) const {
void Movable::FireEngines(const Vector &Direction /*unit vector... might default to "r"*/,
float FuelSpeed,
float FMass) {
Energetic *energetic = dynamic_cast<Energetic*>(this);
FMass = energetic->ExpendFuel(FMass);
Unit *unit = vega_dynamic_cast_ptr<Unit>(this);
FMass = unit->ExpendFuel(FMass);
NetForce += Direction * ((double)FuelSpeed * (double)FMass / GetElapsedTime());
}

Expand Down Expand Up @@ -533,12 +533,12 @@ Vector Movable::MaxTorque(const Vector &torque) {
}

Vector Movable::ClampTorque(const Vector &amt1) {
Energetic *energetic = dynamic_cast<Energetic*>(this);
Unit *unit = vega_dynamic_cast_ptr<Unit>(this);
Vector Res = amt1;

energetic->WCWarpIsFuelHack(true);
unit->WCWarpIsFuelHack(true);

float fuelclamp = (energetic->fuelData() <= 0) ? configuration()->fuel.no_fuel_thrust : 1;
float fuelclamp = (unit->fuel.Level() <= 0) ? configuration()->fuel.no_fuel_thrust : 1;
if (fabs(amt1.i) > fuelclamp * limits.pitch) {
Res.i = copysign(fuelclamp * limits.pitch, amt1.i);
}
Expand All @@ -550,18 +550,18 @@ Vector Movable::ClampTorque(const Vector &amt1) {
}
//1/5,000,000 m/s

energetic->ExpendMomentaryFuelUsage(Res.Magnitude());
energetic->WCWarpIsFuelHack(false);
unit->ExpendMomentaryFuelUsage(Res.Magnitude());
unit->WCWarpIsFuelHack(false);
return Res;
}


Vector Movable::ClampVelocity(const Vector &velocity, const bool afterburn) {
Energetic *energetic = dynamic_cast<Energetic*>(this);
Unit *unit = static_cast<Unit *>(this);
const Unit *unit = vega_dynamic_const_cast_ptr<const Unit>(this);

float fuelclamp = (unit->fuel.Level() <= 0) ? configuration()->fuel.no_fuel_thrust : 1;
float abfuelclamp = (unit->fuel.Level() <= 0 || (unit->energy.Level() < unit->afterburnenergy * static_cast<double>(simulation_atom_var))) ? configuration()->fuel.no_fuel_afterburn : 1;

float fuelclamp = (energetic->fuelData() <= 0) ? configuration()->fuel.no_fuel_thrust : 1;
float abfuelclamp = (energetic->fuelData() <= 0 || (unit->energy.Level() < unit->afterburnenergy * static_cast<double>(simulation_atom_var))) ? configuration()->fuel.no_fuel_afterburn : 1;
float limit =
afterburn ? (abfuelclamp
* (unit->computer.max_ab_speed()
Expand All @@ -576,7 +576,7 @@ Vector Movable::ClampVelocity(const Vector &velocity, const bool afterburn) {


Vector Movable::ClampAngVel(const Vector &velocity) {
Unit *unit = static_cast<Unit *>(this);
const Unit *unit = vega_dynamic_const_cast_ptr<const Unit>(this);

Vector res(velocity);
if (res.i >= 0) {
Expand Down Expand Up @@ -769,7 +769,7 @@ void Movable::RollTorque(float amt) {
}

void Movable::Thrust(const Vector &amt1, bool afterburn) {
Unit *unit = static_cast<Unit *>(this);
Unit *unit = vega_dynamic_cast_ptr<Unit>(this);

if (unit->afterburntype == 0) {
afterburn = afterburn && unit->energy.Level() > unit->afterburnenergy * static_cast<double>(simulation_atom_var);
Expand Down
5 changes: 3 additions & 2 deletions engine/src/cmd/unit_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,8 @@ void Unit::LoadRow(std::string unit_identifier, string modification, bool saved_
computer.radar.trackingcone = cos(UnitCSVFactory::GetVariable(unit_key, "Tracking_Cone", 180.0f) * VS_PI / 180);
computer.radar.lockcone = cos(UnitCSVFactory::GetVariable(unit_key, "Lock_Cone", 180.0f) * VS_PI / 180);

cloak = Cloak(unit_key);
const static bool warp_energy_for_cloak = configuration()->warp_config.use_warp_energy_for_cloak;
cloak = Cloak(unit_key, (warp_energy_for_cloak ? &ftl_energy : &energy));

repair_droid = UnitCSVFactory::GetVariable(unit_key, "Repair_Droid", 0);
ecm = UnitCSVFactory::GetVariable(unit_key, "ECM_Rating", 0);
Expand Down Expand Up @@ -1375,7 +1376,7 @@ string Unit::WriteUnitString() {
unit["Max_Cone"] = tos(acos(computer.radar.maxcone) * 180. / VS_PI);
unit["Lock_Cone"] = tos(acos(computer.radar.lockcone) * 180. / VS_PI);

cloak.Save(unit);
cloak.SaveToCSV(unit);
unit["Repair_Droid"] = tos(repair_droid);
unit["ECM_Rating"] = tos(ecm > 0 ? ecm : -ecm);
unit["Hud_Functionality"] = WriteHudDamage(this);
Expand Down
29 changes: 16 additions & 13 deletions engine/src/cmd/unit_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3384,19 +3384,7 @@ bool Unit::UpAndDownGrade(const Unit *up,
}
}
}
if ((!cloak.Capable() && up->cloak.Capable()) || force_change_on_nothing) {
if (touchme) {
cloak.Enable();

cloak.minimum = up->cloak.minimum;
cloak.rate = up->cloak.rate;
cloak.glass = up->cloak.glass;
cloak.energy = up->cloak.energy;
}
++numave;
} else if (cloak.Capable() && up->cloak.Capable()) {
cancompletefully = false;
}

//NOTE: Afterburner type 2 (jmp)
//NOTE: Afterburner type 1 (gas)
//NOTE: Afterburner type 0 (pwr)
Expand Down Expand Up @@ -4862,3 +4850,18 @@ bool Unit::TransferUnitToSystem(unsigned int kk, StarSystem *&savedStarSystem, b
}
return ret;
}

// TODO: move this to energy or just simplify
// Why single out shields from other constant drains?
float Unit::energyData() const {
float capacitance = totalShieldEnergyCapacitance();

if (configuration()->physics_config.max_shield_lowers_capacitance) {
if (energy.MaxLevel() <= capacitance) {
return 0;
}
return (energy.Level()) / (energy.MaxLevel() - capacitance);
} else {
return energy.Percent();
}
}
7 changes: 6 additions & 1 deletion engine/src/cmd/unit_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ void UncheckUnit( class Unit*un );
#include "SharedPool.h"
#include "role_bitmask.h"
#include "upgradeable_unit.h"
#include "cloak.h"


#include "configuration/configuration.h"
#include "configuration/game_config.h"

#include "cargo_color.h"

// Components
#include "components/cloak.h"
#include "components/energy_container.h"
#include "components/reactor.h"

Expand Down Expand Up @@ -997,6 +998,10 @@ class Unit : public Armed, public Audible, public Drawable, public Damageable, p
// object_a->field_a = object_b->field_b;
float temporary_upgrade_float_variable;


// Python Interfaces
float fuelData() const { return fuel.Level(); }
float energyData() const;
};

Unit *findUnitInStarsystem(const void *unitDoNotDereference);
Expand Down
4 changes: 4 additions & 0 deletions engine/src/cmd/unit_util_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,10 @@ float PercentOperational(Unit *un, std::string name, std::string category, bool
return un->reactor.Damaged() ? 0.5 : 1.0;
}

if(upgrade_category == "Cloak") {
return un->cloak.Damaged() ? 0.5 : 1.0;
}


// Old Code
if (category.find(DamagedCategory) == 0) {
Expand Down
Loading

0 comments on commit cbcae9d

Please sign in to comment.