Skip to content

Commit

Permalink
Remove all usages of FixedGuns
Browse files Browse the repository at this point in the history
  • Loading branch information
sturnclaw committed Dec 7, 2024
1 parent 876eb66 commit efdab2a
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 107 deletions.
1 change: 1 addition & 0 deletions src/Beam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "GameSaveError.h"
#include "JsonUtils.h"
#include "Pi.h"
#include "Projectile.h"
#include "Planet.h"
#include "Player.h"
#include "Sfx.h"
Expand Down
22 changes: 19 additions & 3 deletions src/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,34 @@ class Body : public DeleteEmitter, public PropertiedObject {
T *GetComponent() const
{
auto *type = BodyComponentDB::GetComponentType<T>();
return m_components & (uint64_t(1) << uint8_t(type->componentIndex)) ? type->get(this) : nullptr;
return m_components & GetComponentBit(type->componentIndex) ? type->get(this) : nullptr;
}

// Add a component to this body if it is not already present.
// Returns a pointer to the existing component if a new one could not be added.
template <typename T>
T *AddComponent()
{
auto *type = BodyComponentDB::GetComponentType<T>();
if (m_components & (uint64_t(1) << uint8_t(type->componentIndex)))
if (m_components & GetComponentBit(type->componentIndex))
return type->get(this);

m_components |= (uint64_t(1) << uint8_t(type->componentIndex));
m_components |= GetComponentBit(type->componentIndex);
return type->newComponent(this);
}

// Remove a component from this body, destroying it.
template<typename T>
void RemoveComponent()
{
auto *type = BodyComponentDB::GetComponentType<T>();
if (!(m_components & GetComponentBit(type->componentIndex)))
return;

m_components ^= GetComponentBit(type->componentIndex);
type->deleteComponent(this);
}

// Returns the bitset of components attached to this body. Prefer using HasComponent<> or GetComponent<> instead.
uint64_t GetComponentList() const { return m_components; }

Expand Down Expand Up @@ -218,6 +232,8 @@ class Body : public DeleteEmitter, public PropertiedObject {
matrix3x3d m_interpOrient;

private:
uint64_t GetComponentBit(uint8_t bit) const { return uint64_t(1) << bit; }

vector3d m_pos;
matrix3x3d m_orient;
FrameId m_frame; // frame of reference
Expand Down
1 change: 0 additions & 1 deletion src/DynamicBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "DynamicBody.h"

#include "FixedGuns.h"
#include "Frame.h"
#include "GameSaveError.h"
#include "JsonUtils.h"
Expand Down
2 changes: 0 additions & 2 deletions src/DynamicBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
#include "vector3.h"

class Propulsion;
class FixedGuns;
class Orbit;

class DynamicBody : public ModelBody {
private:
friend class Propulsion;
friend class FixedGuns;

public:
OBJDEF(DynamicBody, ModelBody, DYNAMICBODY);
Expand Down
7 changes: 0 additions & 7 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "Player.h"

#include "FixedGuns.h"
#include "Frame.h"
#include "Game.h"
#include "GameConfig.h"
Expand Down Expand Up @@ -32,15 +31,13 @@ Player::Player(const ShipType::Id &shipId) :
{
SetController(new PlayerShipController());
InitCockpit();
m_fixedGuns->SetShouldUseLeadCalc(true);
m_atmosAccel = vector3d(0.0f, 0.0f, 0.0f);
}

Player::Player(const Json &jsonObj, Space *space) :
Ship(jsonObj, space)
{
InitCockpit();
m_fixedGuns->SetShouldUseLeadCalc(true);
}

void Player::SetShipType(const ShipType::Id &shipId)
Expand Down Expand Up @@ -280,10 +277,6 @@ void Player::StaticUpdate(const float timeStep)
{
Ship::StaticUpdate(timeStep);

for (size_t i = 0; i < GUNMOUNT_MAX; i++)
if (m_fixedGuns->IsGunMounted(i))
m_fixedGuns->UpdateLead(timeStep, i, this, GetCombatTarget());

// now insert the latest value
vector3d current_atmosAccel = GetAtmosForce() * (1.0 / GetMass());
m_atmosJerk = (current_atmosAccel - m_atmosAccel) * Pi::game->GetInvTimeAccelRate();
Expand Down
4 changes: 2 additions & 2 deletions src/Ship-AI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Space.h"
#include "SpaceStation.h"
#include "lua/LuaEvent.h"
#include "ship/GunManager.h"

#include "profiler/Profiler.h"

Expand All @@ -24,8 +25,7 @@ bool Ship::AITimeStep(float timeStep)

// just in case the AI left it on
ClearThrusterState();
for (int i = 0; i < Guns::GUNMOUNT_MAX; i++)
SetGunState(i, 0);
m_gunManager->SetAllGroupsFiring(false);
return true;
}

Expand Down
104 changes: 35 additions & 69 deletions src/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "scenegraph/Animation.h"
#include "scenegraph/Tag.h"
#include "scenegraph/CollisionGeometry.h"
#include "ship/GunManager.h"
#include "ship/PlayerShipController.h"

static const float TONS_HULL_PER_SHIELD = 10.f;
Expand All @@ -52,8 +53,8 @@ Ship::Ship(const ShipType::Id &shipId) :
THIS CODE DOES NOT RUN WHEN LOADING SAVEGAMES!!
*/
m_propulsion = AddComponent<Propulsion>();
m_fixedGuns = AddComponent<FixedGuns>();
m_shields = AddComponent<Shields>();
m_gunManager = AddComponent<GunManager>();
Properties().Set("flightState", EnumStrings::GetString("ShipFlightState", m_flightState));
Properties().Set("alertStatus", EnumStrings::GetString("ShipAlertStatus", m_alertState));

Expand All @@ -78,7 +79,6 @@ Ship::Ship(const ShipType::Id &shipId) :

m_hyperspace.countdown = 0;
m_hyperspace.now = false;
m_fixedGuns->Init(this);
m_ecmRecharge = 0;
m_shieldCooldown = 0.0f;
m_curAICmd = 0;
Expand Down Expand Up @@ -118,8 +118,8 @@ Ship::Ship(const Json &jsonObj, Space *space) :
DynamicBody(jsonObj, space)
{
m_propulsion = AddComponent<Propulsion>();
m_fixedGuns = AddComponent<FixedGuns>();
m_shields = AddComponent<Shields>();
m_gunManager = AddComponent<GunManager>();

try {
Json shipObj = jsonObj["ship"];
Expand Down Expand Up @@ -156,8 +156,6 @@ Ship::Ship(const Json &jsonObj, Space *space) :
m_hyperspace.sounds.abort_sound = shipObj.value("hyperspace_abort_sound", "");
m_hyperspace.sounds.jump_sound = shipObj.value("hyperspace_jump_sound", "");

m_fixedGuns->LoadFromJson(shipObj, space);

m_ecmRecharge = shipObj["ecm_recharge"];
SetShipId(shipObj["ship_type_id"]); // XXX handle missing thirdparty ship
m_dockedWithPort = shipObj["docked_with_port"];
Expand Down Expand Up @@ -227,6 +225,9 @@ void Ship::Init()
p.Set("shieldMassLeft", m_stats.shield_mass_left);
p.Set("fuelMassLeft", m_stats.fuel_tank_mass_left);

// Init of GunManager
m_gunManager->Init(this);

// Init of Propulsion:
m_propulsion->Init(this, GetModel(), m_type->fuelTankMass, m_type->effectiveExhaustVelocity, m_type->linThrust, m_type->angThrust, m_type->linAccelerationCap);

Expand All @@ -238,8 +239,6 @@ void Ship::Init()
m_landingGearAnimation = GetModel()->FindAnimation("gear_down");
m_forceWheelUpdate = true;

m_fixedGuns->InitGuns(GetModel());

// If we've got the tag_landing set then use it for an offset
// otherwise use zero so that it will dock but look clearly incorrect
const SceneGraph::Tag *tagNode = GetModel()->FindTagByName("tag_landing");
Expand Down Expand Up @@ -286,8 +285,6 @@ void Ship::SaveToJson(Json &jsonObj, Space *space)
shipObj["hyperspace_abort_sound"] = m_hyperspace.sounds.abort_sound;
shipObj["hyperspace_jump_sound"] = m_hyperspace.sounds.jump_sound;

m_fixedGuns->SaveToJson(shipObj, space);

shipObj["ecm_recharge"] = m_ecmRecharge;
shipObj["ship_type_id"] = m_type->id;
shipObj["docked_with_port"] = m_dockedWithPort;
Expand Down Expand Up @@ -626,7 +623,6 @@ void Ship::UpdateEquipStats()
p.Set("shieldMass", m_stats.shield_mass);

UpdateFuelStats();
UpdateGunsStats();

unsigned int thruster_power_cap = p.Get("thruster_power_cap");
const double power_mul = m_type->thrusterUpgrades[Clamp(thruster_power_cap, 0U, 3U)];
Expand Down Expand Up @@ -664,43 +660,6 @@ void Ship::UpdateLuaStats()
p.Set("maxHyperspaceRange", m_stats.hyperspace_range_max);
}

void Ship::UpdateGunsStats()
{
PropertyMap &prop = Properties();
float cooler = prop.Get("laser_cooler_cap");
m_fixedGuns->SetCoolingBoost(cooler ? cooler : 1.0f);

for (int num = 0; num < 2; num++) {
std::string prefix(num ? "laser_rear_" : "laser_front_");
int damage = prop.Get(prefix + "damage");
if (!damage) {
m_fixedGuns->UnMountGun(num);
} else {
const Color c(
prop.Get(prefix + "rgba_r"),
prop.Get(prefix + "rgba_g"),
prop.Get(prefix + "rgba_b"),
prop.Get(prefix + "rgba_a"));
const float heatrate = prop.Get(prefix + "heatrate").get_number(0.01f);
const float coolrate = prop.Get(prefix + "coolrate").get_number(0.01f);
const float lifespan = prop.Get(prefix + "lifespan");
const float width = prop.Get(prefix + "width");
const float length = prop.Get(prefix + "length");
const float speed = prop.Get(prefix + "speed");
const float recharge = prop.Get(prefix + "rechargeTime");
const bool mining = prop.Get(prefix + "mining").get_integer();
const bool beam = prop.Get(prefix + "beam").get_integer();

m_fixedGuns->MountGun(num, recharge, lifespan, damage, length, width, mining, c, speed, beam, heatrate, coolrate);

if (prop.Get(prefix + "dual").get_integer())
m_fixedGuns->IsDual(num, true);
else
m_fixedGuns->IsDual(num, false);
}
}
}

void Ship::UpdateFuelStats()
{
m_stats.fuel_tank_mass_left = m_propulsion->FuelTankMassLeft();
Expand Down Expand Up @@ -1103,7 +1062,7 @@ void Ship::UpdateAlertState()
if (GetPositionRelTo(ship).LengthSqr() < ALERT_DISTANCE * ALERT_DISTANCE) {
ship_is_near = true;

Uint32 gunstate = ship->m_fixedGuns->IsFiring();
Uint32 gunstate = ship->m_gunManager->IsFiring();
if (gunstate) {
ship_is_firing = true;
break;
Expand Down Expand Up @@ -1274,36 +1233,38 @@ void Ship::StaticUpdate(const float timeStep)
m_launchLockTimeout = 0;

// lasers
FixedGuns *fg = m_fixedGuns;
fg->UpdateGuns(timeStep);
for (int i = 0; i < 2; i++) {
if (fg->Fire(i, this)) {
if (fg->IsBeam(i)) {
float vl, vr;
Sound::CalculateStereo(this, 1.0f, &vl, &vr);
m_beamLaser[i].Play("Beam_laser", vl, vr, Sound::OP_REPEAT);
} else {
Sound::BodyMakeNoise(this, "Pulse_Laser", 1.0f);
}
LuaEvent::Queue("onShipFiring", this);
}
GetComponent<GunManager>()->StaticUpdate(timeStep);

if (fg->IsBeam(i)) {
if (fg->IsFiring(i)) {
// TODO: this is abominable.
// It will lead to multiple sound cutouts with more than a single beam laser
// Unfortunately, I don't have the time or inclination to completely rewrite the sound system at this juncture

GunManager::WeaponIndexSet firedGuns = m_gunManager->GetGunsFiredThisFrame();
GunManager::WeaponIndexSet stoppedGuns = m_gunManager->GetGunsStoppedThisFrame();

for (size_t i = 0; i < m_gunManager->GetNumWeapons(); i++) {
const GunManager::WeaponState *ws = m_gunManager->GetWeaponState(i);
if (ws->data.projectileType == GunManager::PROJECTILE_BEAM) {
if (m_gunManager->GetWeaponGroups()[ws->group].firing) {
float vl, vr;
Sound::CalculateStereo(this, 1.0f, &vl, &vr);
if (!m_beamLaser[i].IsPlaying()) {
m_beamLaser[i].Play("Beam_laser", vl, vr, Sound::OP_REPEAT);
if (firedGuns[i]) {
m_beamLaser[i % 2].Play("Beam_laser", vl, vr, Sound::OP_REPEAT);
} else {
// update volume
m_beamLaser[i].SetVolume(vl, vr);
m_beamLaser[i % 2].SetVolume(vl, vr);
}
} else if (!fg->IsFiring(i) && m_beamLaser[i].IsPlaying()) {
m_beamLaser[i].Stop();
} else if (stoppedGuns[i]) {
m_beamLaser[i % 2].Stop();
}
} else if (firedGuns[i]) {
Sound::BodyMakeNoise(this, "Pulse_Laser", 1.0f);
}
}

if (firedGuns.any())
LuaEvent::Queue("onShipFiring", this);

if (m_ecmRecharge > 0.0f) {
m_ecmRecharge = std::max(0.0f, m_ecmRecharge - timeStep);
}
Expand Down Expand Up @@ -1414,7 +1375,7 @@ void Ship::SetGunState(int idx, int state)
if (m_flightState != FLYING)
return;

m_fixedGuns->SetGunFiringState(idx, state);
m_gunManager->SetGroupFiring(idx, state);
}

bool Ship::SetWheelState(bool down)
Expand Down Expand Up @@ -1572,6 +1533,11 @@ void Ship::SetShipType(const ShipType::Id &shipId)
SetModel(m_type->modelName.c_str());
SetupShields();

// Recreate our GunManager for the new ship type
// Its init method will be called later
RemoveComponent<GunManager>();
m_gunManager = AddComponent<GunManager>();

m_skin.SetDecal(m_type->manufacturer);
m_skin.Apply(GetModel());
Init();
Expand Down
7 changes: 3 additions & 4 deletions src/Ship.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "scenegraph/ModelSkin.h"
#include "sound/Sound.h"

#include "FixedGuns.h"
#include "ship/Propulsion.h"

class AICommand;
Expand All @@ -27,6 +26,7 @@ class Planet;
class Sensors;
class ShipController;
class Space;
class GunManager;

struct CollisionContact;
struct HeatGradientParameters_t;
Expand Down Expand Up @@ -96,12 +96,11 @@ class Ship : public DynamicBody {
void UpdateLuaStats();
void UpdateEquipStats();
void UpdateFuelStats();
void UpdateGunsStats();
const shipstats_t &GetStats() const { return m_stats; }

void Explode();
virtual bool DoDamage(float kgDamage); // can be overloaded in Player to add audio
void SetGunState(int idx, int state);
[[deprecated]] void SetGunState(int idx, int state);
void UpdateMass();
virtual bool SetWheelState(bool down); // returns success of state change, NOT state itself
virtual bool ManualDocking() const { return false; }
Expand Down Expand Up @@ -273,7 +272,7 @@ class Ship : public DynamicBody {
} m_hyperspace;

Propulsion *m_propulsion;
FixedGuns *m_fixedGuns;
GunManager *m_gunManager;
Shields *m_shields;

private:
Expand Down
Loading

0 comments on commit efdab2a

Please sign in to comment.